检查多个条件
- 使用
and
检查多个条件
age_0 = 22
age_1 = 18
print(age_0 >= 21 and age_1 >= 21 )
age_1 = 22
print(age_0 >= 21 and age_1 >= 21)
- 使用
or
检查多个条件
age_0 = 22
age_1 = 18
print(age_0 >= 21 or age_1 >= 21 )
age_1 = 22
print(age_
- 要判断特定的值是否已包含在列表中,可使用关键字
in
requested_toppings = ['mushrooms', 'onions', 'pineapple']
print('mushrooms' in requested_toppings)
print('pepperoni' in requested_toppings)
- 检查特定值是否不包含在列表中,可使用关键字
not in
banned_users = ['andrew', 'carolina', 'david']
user = 'marie'
if user not in banned_users:
print(user.title() + ", you can post a response if you wish.")
- 布尔表达式
与条件表达式一样,布尔表达式的结果要么为True ,要么为False
game_active = True
can_edit = False
注:函数lower()
:可将变量的值转换为小写,同时不会修改存储在变量 中的值
car = 'Audi'
print(car.lower())
print(car)