第5章 if语句
5.1简单例子
cars = ['audi','bmw','subaru','toyota']
for car in cars:
if car == 'bmw':
print(car.upper())
else:
print(car.title())
5.2 条件测试
5.2.2 检查是否相等时不考虑大小写 使用.lower()
5.2 3检查是否不相等 使用不等号
requested_topping='mushrooms'
if requested_topping!='anchovies':
print("Hold the anchovies!")
5.2.4 检查数字
使用==,<=,>=,!=号
5.2.5 检查多个条件 使用and 或or
5.2.6检查特定的值是否包含在列表中 使用in
5.2.7检查特定的值是否不包含在列表中 使用not in
5.2.8 布尔表达式
5.3 if语句
5.3.1 简单的if语句
5.3.2 if-else语句
5.3.3 if-elif-else语句
5.3.4 使用多个elif代码块
5.3.5 elif后可省略else
5.4 使用if语句处理列表
5.4.1 检查特殊元素
可选方案:for循环中使用if语句
5.4.2 确定列表不是空的
if requested_toppings:
do something
else:
do something
其中requested_toppings 是列表
5.4.3 使用多个列表