在 Python 的 if 语句中检查多个条件

在 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和存储bc值是否大于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如果可迭代对象中的所有元素都为真(或可迭代对象为空)则返回。

如果列表中的所有条件都计算为Trueif则块运行,否则,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 at least one of the conditions is met, the 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.

main.py
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.

When the first condition isn’t met and the boolean AND operator is used, the second condition isn’t evaluated at all.
main.py
# 👇️ 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: