在 Python 的 if 语句中检查多个条件
Check for multiple conditions in an if statement in Python
使用布尔and
运算符检查 if 语句中的多个条件。
if
只有满足所有条件,该块才会运行。
a = 1 b = 3 c = 7 # ✅ check for multiple conditions using AND if a == 1 and b == 3 and c == 7: # 👇️ this runs print('All conditions in the if statement are met') else: print('Not all conditions in the if statement are met') # -------------------------------------------- # ✅ check for multiple conditions using OR if a == 10 or b == 30 or c == 7: # 👇️ this runs print('One or more conditions in the if statement are met') else: print('None of the conditions in the if statement is met')
第一个示例使用布尔and
运算符来检查if
语句中的多个条件。
if
块仅在满足所有条件时运行,否则该else
块运行。您可以在条件中使用任何比较运算符。
a = 1 b = 3 c = 7 if a > 0 and b > 0 and c > 0: # 👇️ this runs print('All conditions in the if statement are met') else: print('Not all conditions in the if statement are met')
该示例检查变量a
和存储b
的c
值是否大于0
。
False
,则解释器会短路并且不会计算下一个条件。 使用all()
函数检查多个条件if
或者,您可以使用该all()
功能。
a = 1 b = 3 c = 7 if all([a > 0, b > 0, c > 0]): # 👇️ this runs print('All conditions in the if statement are met') else: print('Not all conditions in the if statement are met')
我们将包含多个条件的列表传递给函数all()
。
all ()内置函数接受一个可迭代对象作为参数,True
如果可迭代对象中的所有元素都为真(或可迭代对象为空)则返回。
如果列表中的所有条件都计算为True
,if
则块运行,否则,else
块运行。
如果不满足其中一个条件,all()
函数将短路,else
块将运行。
使用布尔或检查多个条件
如果您需要检查是否至少满足一个条件,请使用
布尔 OR运算符。
if
如果满足至少一个条件,该块将运行。
a = 1 b = 3 c = 7 # ✅ check for multiple conditions using OR if a == 10 or b == 30 or c == 7: # 👇️ this runs print('One or more conditions in the if statement are met') else: print('None of the conditions in the if statement is met')
or
运算符时,if
如果至少满足一个条件,该块就会运行。 使用any()
函数检查多个条件
或者,您可以使用该any()
函数来检查语句中是否至少满足多个条件中的一个if
。
a = 1 b = 3 c = 7 if any([a > 100, b > 100, c > 6]): # 👇️ this runs print('All conditions in the if statement are met') else: print('Not all conditions in the if statement are met')
我们将包含多个条件的列表传递给函数any()
。
any函数接受一个可迭代对象作为参数,如果可迭代对象中的任何元素为真则返回。True
if
block runs, otherwise the else
block runs.If one of the conditions is met, the any()
function will short-circuit
returning True
and the if
block will run.
If none of the conditions is met, the any()
function will return False
and
the else
block will run.
# Mixing the boolean AND and boolean OR operators
Here is an example of mixing the boolean AND and
boolean OR operators.
num_1 = 50 num_2 = 75 if (num_1 > 100 and num_1 < 500) or num_2 > num_1: # 👇️ this runs print('if block runs') else: print('else block runs')
The code between the parentheses checks if the num_1
variable stores a number
that is greater than 100
and the number is less than 500
.
The first condition isn’t met, so the expression evaluates to False
.
# 👇️ this never runs (num_1 > 100 and num_1 < 500)
Then the boolean OR operator evaluates the condition to the right.
The condition checks if the num_2
variable is greater than num_1
.
The condition is met, so the if
block runs.
# Additional Resources
You can learn more about the related topics by checking out the following
tutorials:
- if-elif-else statement on one line in Python
- Styling multiline ‘if’ statements in Python
- Get the first item in a list that matches condition – Python
- Find the index of Elements that meet a condition in Python
- Using f-string for conditional formatting in Python
- Check if all/any elements in List meet condition in Python