5.1简单示例
cars = {"audi", "bmw", "subaru", "toyota"}
for car in cars:
if car == "bmw":
print(car.upper())
else:
print(car)
5.2条件测试
5.2.1检查是否相等
car = "audi" print(car == "BMW")
5.2.2检查是否相等时忽略大小写
Python中检查是否相等时区分大小写。例如,两个大小写不同的值被视为不相等:
car = "Audi"
car == "audi"
5.2.3检查是否不相等
requsted_topping = "mushrooms"
if requsted_topping != "anchovies":
print("hold the anchovies") #程序执行结果为两者不相等,输出结果
5.2.4数值比较
age = 18 age ==19
5.2.5检查多个条件
1.使用and检查多个条件
age_0 =22 age_1 =18 print(age_0>=21 and age_1>=21)
2.使用or检查多个条件
age_0 =22 age_1 =18 print(age_0>=21 or age_1>=21) #有一个为真则为真
5.2.6检查特定值是否包含在列表中
要判断特定的值是否包含在列表中,可使用关键字 in
requsted_topping = ["mushroom", "onions", "pineapple"]
print("onions" in requsted_topping)
结果为True
5.2.7检查特定值是否不包含在列表中
requsted_topping = ["mushroom", "onions", "pineapple"]
print("onions" not in requsted_topping)
5.2.8布尔表达式
布尔表达式不过是测试条件的别名,其结果要么为True,要么为False
game_active = True can_edit = False
5.3if语句
5.3.1最简单的if语句
age = 18
if age>=19:
printf("111")
5.3.2if-else语句
age = 17
if age >= 18:
print("You are old enough to vote")
else:
print("You are too young to vote")
5.3.3if-elif-else结构
age = 12
if age < 4:
print("Your admission cost is $0")
elif age < 18:
print("Your admission cost is 25$")
else:
print("Your admission cost is $40")
##
age = 12
if age < 4:
price = 0
elif age < 18:
price = 25
else:
price = 40
print(f"Your admission cost is ${price}")
#这个代码与前面的例子相同,但其结构的作用更小:它只确定门票价格,修订后的代码更容易修改,只需修改一个而不是三个函数调用print()
5.3.4使用多个elif代码块
age = 12
if age < 4:
price = 0
elif age < 18:
price = 25
elif age < 65:
price = 40
else:
price = 20
print(f"Your admission cost is ${price}")
5.3.5省略else代码块
Python并不要求if-elif后面必须有else代码块。在有些情况下,使用else代码块很有用
age = 12
if age < 4:
price = 0
elif age < 18:
price = 25
elif age < 65:
price = 40
elif age > 65: #else是一条保罗万象的语句,这样写可能会引入无效或者恶意的数据
price = 20
print(f"Your admission cost is ${price}")
5.3.6测试多个条件
对于满足多个条件的结构,使用if-elif-else的话,一旦遇到满足条件的,就会跳过其余测试
因此对于这种情况,就需要使用一系列独立的if语句。
5.4使用if语句处理特殊列表
5.4.1检查特殊元素
requested_toppings = ["mushrooms", "green peppers", "extra cheese"]
for requested_topping in requested_toppings:
print(f"Adding {requested_topping}")
print("nFinishing making your pizza!")
#将所需的配料放到列表之中,并使用一个循环来支出添加到披萨中的配料。
然而,当列表中的青椒用完了,该如何处理呢,此时可以添加一个if语句
print(f"Your admission cost is ${price}")
requested_toppings = ["mushrooms", "green peppers", "extra cheese"]
for requested_topping in requested_toppings:
if requested_topping == "green peppers":
print("Green peppers has sold out!")
else:
print(f"Adding {requested_topping}")
print("nFinishing making your pizza!")
5.4.2确定列表不是空的
在运行之前检查 列表是否为空很重要
requested_toppings = []
if requested_toppings: #if后面的列表至少包含一个元素时返回True
for requested_topping in requested_toppings:
print(f"Adding {requested_topping}")
print("Finishing making your pizza")
else: #并在列表为空时返回False
print("Are you sure you want a pizza?")
5.4.3使用多个 列表
available_topping = ["mushrooms", "olives", "green peppers", "pepperoin", "pineapple", "extra cheese"]
requested_toppings = ["mushrooms", "french fries", "extra cheese"]
for requested_topping in requested_toppings:
if requested_topping in available_topping:
print(f"Adding {requested_topping}")
else:
print(f"Sorry,we don't have {requested_topping}")
print("nFinished making your pizza")



