Article Directory
if infrastructure
single if statement
if condition:
executes a block of code when the condition is true
When the condition is true, the corresponding block of code is executed.
if-else statement
if condition:
# execute the code block when the condition is true
else:
# execute the code block when the condition is false
When the condition is true, the first block of code is executed; when the condition is false, the second block of code is executed.
if-elif-else statement
if condition 1:
# Execute the code block when condition 1 is true
elif Condition 2:
# Execute the code block when condition 2 is true
elif Condition 3:
# Execute the code block when condition 3 is true
...
else:
# Execute all the above conditions code block when dissatisfied
When condition 1 is true, execute the first block of code; otherwise, keep checking condition 2. If condition 2 is true, the second code block is executed; and so on. If all conditions are not met, the else code block is executed.
nested if statement
if 条件1:
# 执行条件1为真时的代码块
if 条件2:
# 执行条件1和条件2同时满足时的代码块
else:
# 执行条件1为真而条件2不满足时的代码块
else:
# 执行条件1不满足时的代码块
You can nest another if statement inside an if code block to handle more complex conditional logic.
advanced usage of if
use comparison operators
if a == b:
# block of code executed when a is equal to b
if a != b:
# block of code executed when a is not equal to b
if a > b:
# block of code executed when a is greater than b
if a < b:
# block of code executed when a is less than b
if a >= b:
# Code block executed when a is greater than or equal to b
if a <= b:
# block of code executed when a is less than or equal to b
Comparison operators are used in conditional statements to compare the magnitude relationship of two values.
use logical operators
if condition 1 and condition 2:
# code block executed when condition 1 and condition 2 are met at the same time
if condition 1 or condition 2:
# block of code executed when one of condition 1 or condition 2 is met
if not condition:
# block of code to execute when the condition is not met
Logical operators are used to combine multiple conditions to perform compound condition judgments.
use the in keyword
if element in iterable:
# block of code executed when the element exists in the iterable
The in keyword is used to check whether an element exists in an iterable object, often used in conjunction with range
Common iterable objects: list (List), tuple (Tuple), range object, etc.
range() is a built-in function in Python that generates a sequence of integers within a specified range. It is often used in loop structures, such as for loops, to iteratively generate integers according to the specified start value, end value, and step size.
range() function
range(start, stop, step)
Parameter Description:
start: optional, indicating the start value of the sequence, the default is 0.
stop: Required, indicating the stop value of the sequence (not included in the sequence).
step: optional, indicating the step size (default is 1). The step size can be a positive integer, a negative integer, or zero.
The range() function returns an iterable object representing a sequence of integers, and each element in the sequence can be obtained by traversing the iterable object.
Example:
# 生成从 0 到 4 的整数序列
for num in range(5):
print(num) # 输出:0, 1, 2, 3, 4
# 生成从 2 到 9(不包含 9)的整数序列,步长为 3
for num in range(2, 9, 3):
print(num) # 输出:2, 5, 8
# 生成递减的整数序列,从 10 到 1(不包含 1)
for num in range(10, 1, -1):
print(num) # 输出:10, 9, 8, 7, 6, 5, 4, 3, 2
It should be noted that the sequence generated by the range() function does not contain the termination value. If you need to include the termination value, you can use stop + 1 in the loop or convert to other data structures such as lists.
use the is keyword
The is keyword is used to check if two objects refer to the same memory address.
if 对象 is None:
# 当对象为 None 时执行的代码块
Use the pass statement
if 条件:
pass
The pass statement does nothing and is often used as a placeholder for code that needs to be added later.
Ternary operator
三目运算符
A concise version of Python条件表达式
, also known as条件运算符
.
grammar
[value to return if condition is met] if [condition] else [value to return if condition is not met]
where [condition] is an expression that evaluates to a Boolean value. If the condition is true, returns [the value to return if the condition is met]; otherwise returns [the value to return if the condition is not met].
example
x = 10
result = "正数" if x > 0 else "非正数"
print(result) # 输出:正数
In the above example, if x is greater than 0, then "positive number" will be assigned to result; otherwise, "non-positive number" will be assigned to result.
Notice
Using the ternary operator can simplify the code, especially for simple conditional judgment scenarios. But beware, overuse can lead to hard-to-understand code.
Replenish
There is no officially supported quadruple operator in Python. The ternary operator is in 唯一
Python 条件表达式
. It can choose to return different values based on condition in one expression.
The quadruplet is not a built-in syntax of Python, but you can use other ways to achieve its function.
A common approach is to 嵌套的三目运算符
simulate the quadruple operator using
example
x = 10
result = "正数" if x > 0 else ("零" if x == 0 else "负数")
print(result) # 输出:正数
In the above example, we use nested ternary operators to achieve the effect of a quadruple operator. First judge x > 0, if true, return "positive number"; otherwise continue to judge x ==
0, if true, return "zero"; otherwise return "negative number".
Notice
Although this method can achieve a function similar to the quadruplet operator, nested ternary operators can easily make the code difficult to understand and debug. Therefore, in actual programming, it is recommended to use a clearer and easier-to-understand way to express conditional judgment logic, such as using the
if...elif...else structure.