# ★判断语句:if
cars = ['audi', 'bmw', 'subaru', 'toyota']
for car in cars:
if car == 'bmw':
print(car.upper())
else:
print(car.title())
# ★判断是否相等区分大小写
car = 'audi'
print(car == 'Audi') # 结果为False
# 上述问题如果大小写无关紧要,可以使用car.upper()或car.lower()后进行判断
# 判断是否不想等(!=)
car = 'audi'
print(car != 'Audi') # 结果为True
# ★检查多个条件:且(and)
aaa = 18
if aaa > 10 and aaa < 20:
print(aaa)
else:
print('error')
# ★检查多个条件:或(or)
aaa = 1
if aaa > 10 or aaa < 20:
print(aaa)
else:
print('error')
# ★检查特定值是否在列表中(in/not in)
cars = ['audi', 'bmw', 'subaru', 'toyota']
print('audi' in cars) # True
print('AUDI' in cars) # False
print('======')
print('audi' not in cars) # False
print('AUDI' not in cars) # True
# ★if-elif-else结构
score = 88
if score < 60:
print('C')
elif 60 <= score < 80:
print('B')
else:
print('A')
# 并不一定需要(else)
python笔记-005-判断
最新推荐文章于 2024-04-19 16:48:12 发布