- 使用 and 检查多个条件
age_0 = 22
age_1 = 28
if age_0 >= 21 and age_1 >= 21:
print("age_0 >= 21 and age_1 >= 21")
else:
print("age_0 < 21 or age_1 < 21")
- 使用 or 检查多个条件
age_0 = 22
age_1 = 18
if age_0 >= 21 or age_1 >= 21:
print("age_0 >= 21 or age_1 >= 21")
else:
print("age_0 < 21 and age_1 < 21")
- 检查特定值是否包含在列表中
requested_toppings = ['mushrooms', 'onions', 'pineapple']
if 'mushrooms' in requested_toppings:
print("'mushrooms' in requested_toppings")
else:
print("'mushrooms' not in requested_toppings")
- 检查特定值是否不包含在列表中
requested_toppings = ['mushrooms', 'onions', 'pineapple']
if 'mushroom' not in requested_toppings:
print("'mushroom' not in requested_toppings")
else:
print("'mushroom' in requested_toppings")
- 确定列表不是空的
requested_toppings = ['mushrooms', 'onions', 'pineapple']
if requested_toppings: # 判断列表是否为空
if 'mushrooms' in requested_toppings:
print("'mushrooms' in requested_toppings")
else:
print("'mushrooms' not in requested_toppings")
本文参考自 《Python 编程:从入门到实践》