条件测试:
每条if 语句的核心都是一个值为True 或False 的表达式,这种表达式被称为条件测试 。Python根据条件测试的值为True 还是False 来决定是否执行if 语句中的代码。如果条件测试的值为True ,Python就执行紧跟在if 语句后面的代码;如果为False ,Python就忽略这些代码。
1.检查是否相等
大多数条件测试都将一个变量的当前值同特定值进行比较。
要判断两个值是否不等,可结合使用惊叹号和等号(!= )
2.比较数字
条件语句中可包含各种数学比较,如小于、小于等于(<=)、大于、大于等于(>=)
3.检查多个条件
使用and 检查多个条件
使用or 检查多个条件
检查特定值是否包含在列表中
要判断特定的值是否已包含在列表中,可使用关键字in
>>> 'mushrooms' in requested_toppings
True
检查特定值是否不包含在列表中
还有些时候,确定特定的值未包含在列表中很重要;在这种情况下,可使用关键字not in 。
4.布尔表达式
与条件表达式一样,布尔表达式的结果要么为True ,要么为False 。
布尔值通常用于记录条件
在跟踪程序状态或程序中重要的条件方面,布尔值提供了一种高效的方式。
5. if-else 语句
经常需要在条件测试通过了时执行一个操作,并在没有通过时执行另一个操作;在这种情况下,可使用Python提供的if-else 语句。
if age >= 18:
print("You are old enough to vote!")
print("Have you registered to vote yet?")
else:
print("Sorry, you are too young to vote.")
print("Please register to vote as soon as you turn 18!")
6.if-elif-else 结构
经常需要检查超过两个的情形,为此可使用Python提供的if-elif-else 结构。Python只执行if-elif-else 结构中的一个代码块,它依次检查每个条件测试,直到遇到通过了的条件测试。测试通过后,Python将执行紧跟在它后面的代码,并跳过余下的测试。
age = 12
if age < 4:
print("Your admission cost is $0.")
elif age < 18:
print("Your admission cost is $5.")
else:
print("Your admission cost is $10.")
7.省略else 代码块
Python并不要求if-elif 结构后面必须有else 代码块。在有些情况下,else 代码块很有用;而在其他一些情况下,使用一条elif 语句来处理特定的情形更清晰:
age = 12
if age < 4:
price = 0
elif age < 18:
price = 5
elif age < 65:
price = 10
elif age >= 65:
price = 5
print("Your admission cost is $" + str(price) + ".")
8.测试多个条件
requested_toppings = ['mushrooms', 'extra cheese']
if 'mushrooms' in requested_toppings:
print("Adding mushrooms.")
if 'pepperoni' in requested_toppings:
print("Adding pepperoni.")
if 'extra cheese' in requested_toppings:
print("Adding extra cheese.")
print("\nFinished making your pizza!")
如果像使用if-elif-else 结构,代码将不能正确地运行,因为有一个测试通过后,就会跳过余下的测试
第一个测试检查列表中是否包含’mushrooms’ ,它通过了,因此将在比萨中添加蘑菇。然而,Python将跳过if-elif-else 结构中余下的测试,不再检查列表中是否包含’extra cheese’ 和’pepperoni’ 。其结果是,将添加顾客点的第一种配料,但不会添加其他的配料。
总之,如果你只想执行一个代码块,就使用if-elif-else 结构;如果要运行多个代码块,就使用一系列独立的if 语句。
8.使用if 语句处理列表
检查特殊元素:
requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
for requested_topping in requested_toppings:
if requested_topping == 'green peppers':
print("Sorry, we are out of green peppers right now.")
else:
print("Adding " + requested_topping + ".")
print("\nFinished making your pizza!")
确定列表不是空的
下面在制作比萨前检查顾客点的配料列表是否为空。如果列表是空的,就向顾客确认他是否要点普通比萨;如果列表不为空,就像前面的示例那样制作比萨:
requested_toppings = []
if requested_toppings:
for requested_topping in requested_toppings:
print("Adding " + requested_topping + ".")
print("\nFinished making your pizza!")
else:
print("Are you sure you want a plain pizza?")
在if 语句中将列表名用在条件表达式中时,Python将在列表至少包含一个元素时返回True ,并在列表为空时返回False。
使用多个列表
下面的示例定义了两个列表,其中第一个列表包含比萨店供应的配料,而第二个列表包含顾客点的配料。这次对于requested_toppings 中的每个元素,都检查它是否是比萨店供应的配料,再决定是否在比萨中添加它:
available_toppings = ['mushrooms', 'olives', 'green peppers',
'pepperoni', 'pineapple', 'extra cheese']
requested_toppings = ['mushrooms', 'french fries', 'extra cheese']
for requested_topping in requested_toppings:
if requested_topping in available_toppings:
print("Adding " + requested_topping + ".")
else:
print("Sorry, we don't have " + requested_topping + ".")
print("\nFinished making your pizza!")