第5章. if 语句
- 5.1 一个简单示例
- 5.2 条件测试
- 5.2.1 检查是否相等
- 5.2.2 检查是否相等时忽略大小写
- 5.2.3 检查是否不相等
- 5.2.4 数值比较
- 5.2.5 检查多个条件
- 5.2.6 检查特定值是否包含在列表中
- 5.2.7 检查特定值是否不包含在列表中
- 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 省略 else 代码块
- 5.3.6 测试多个条件
- 5.4 使用 if 语句处理列表
- 5.4.1 检查特殊元素
- 5.4.2 确定列表不是空的
- 5.4.3 使用多个列表
- 5.5 设置 if 语句的格式
- 5.1 一个简单的示例
1 cars = ['audi','bmw','subaru','toyota']
2 for car in cars:
3 if car == 'bmw':
4 print(car.upper())
5 else:
6 print(car.title())
运行结果:
- 5.2 条件测试
每条 if 语句的核心都是一个值为 True 或 False 的表达式,这种表达式称为条件测试。Python 根据条件测试的值为 True 还是 False 来决定是否执行 if 语句中的代码。如果条件测试的值为 True,Python 就执行紧跟在 if 语句后面的代码;如果为 False,Python 就忽略这些代码。
- 5.2.1 检查是否相等
相等运算符( == )在两边的值相等时返回 True,否则返回 False。
1 car = 'audi'
2 print(car == 'bmw')
3 print(car == 'audi')
运行结果:
- 5.2.2 检查是否相等时忽略大小写
在 Python 中检查是否相等时区分大小写。
- 5.2.3 检查是否不相等
!=
- 5.2.4 数值比较
==,<,<=,>,>=
- 5.2.5 检查多个条件
要检查是否两个条件都为 True,可使用关键字 and 将两个条件测试合而为一。
为改善可读性,可将每个测试分别放在一对圆括号内,但并非必须这样做。
关键字 or 也能够让你检查多个条件,但只要至少一个条件满足,就能通过整个测试。仅当多个测试都没通过时,使用 or 的表达式才为 False。
1 age_0 = 22
2 age_1 = 18
3 print((age_0 >= 21) and (age_1 >= 21))
4 print((age_0 >= 21) or (age_1 >= 21))
运行结果:
- 5.2.6 检查特定值是否包含在列表中
要判断特定的值是否已包含在列表中,可使用关键字 in。
1 requested_toppings = ['mushrooms','onions','pineapple']
2 print('mushrooms' in requested_toppings)
3 print('pepperoni' in requested_toppings)
运行结果:
- 5.2.7 检查特定值是否不包含在列表中
关键字 not in。
1 requested_toppings = ['mushrooms','onions','pineapple']
2 print('mushrooms' not in requested_toppings)
3 print('pepperoni' not in requested_toppings)
运行结果:
- 5.2.8 布尔表达式
与条件表达式一样,布尔表达式的结果要么为 True,要么为 False。
1 game_active = True # 1行和2行就是布尔表达式
2 can_edit = False
3 print(game_active)
4 print(can_edit)
运行结果:
- 5.3.1 简单的 if 语句
语法:
1 if conditional_test:
2 do something
- 5.3.2 if-else 语句
语法:
1 if conditional_test:
2 do something
3 else:
4 do something_else
- 5.3.3 if-elif-else 结构
Python 只执行 if-elif-else 结构中的一个代码块。它依次检查每个条件测试,直到遇到通过了的条件测试(如果没有通过的,那就不执行所有)。
示例:
1 age = 12
2
3 if age < 4:
4 price = 0
5 elif age < 18:
6 price = 25
7 else:
8 price = 40
9
10 print(f"Your admission cost is ${price}.")
运行结果:
- 5.3.5 省略 else 代码块
Python 并不要求 if-else 结构后面必须有 else 代码块。
- 5.4.1 检查特殊元素
示例(情景:比萨店的青椒用完了)(这节没有新知识):
1 requested_toppings = ['mushrooms','green peppers','extra cheese']
2
3 for requested_topping in requested_toppings:
4 if requested_topping == 'green peppers':
5 print("Sorry, we are out of green peppers right now.")
6 else:
7 print(f"Adding {requested_topping}.")
8
9 print("\nFinished making your pizza!")
运行结果:
- 5.4.2 确定列表不是空的
在 if 语句中将列表名用作条件表达式时,Python 将在列表至少包含一个元素时返回 True,并在列表为空时返回 False。
1 requested_toppings = []
2 if requested_toppings:
3 for requested_topping in requested_toppings:
4 print(f"Adding {requested_topping}.")
5 print("\nFinished making your pizza!")
6 else:
7 print("Are you sure you want a plain pizza?")
运行结果:
- 5.4.3 使用多个列表
示例(这节没有新知识):
1 available_toppings = ['mushrooms','olivers','green peppers','pepperoni','pineapple','extra cheese']
2 requested_toppings = ['mushrooms','french fries','extra cheese']
3
4 for requested_topping in requested_toppings:
5 if requested_topping in available_toppings:
6 print(f"Adding {requested_topping}.")
7 else:
8 print(f"Sorry, we don't have {requested_topping}.")
9
10 print("\nFinished making your pizza!")
运行结果:
- 5.5 设置 if 语句的格式
在诸如 ==、>=和<=等比较运算符两边各添加一个空格。
(〃>_<;〃)(〃>_<;〃)(〃>_<;〃)