if语句
1.简单示例
2.条件测试
2.1是否相等(==)
检查是否相等时区分大小写。
car='bmw'
car=='bmw'
True
car=='audi'
False
2.2是否不相等(!=)
requested_topping='mushrooms'
if requested_topping != 'anchovies':
print("Hold the anchovies")
#Hold the anchovies
2.3比较数字
== 等于
!= 不等于
< 小于 > 大于
<=小于等于 >=大于等于
2.4检查多个条件
2.4.1使用and
只要一个没通过,整个表达式就为False
age_0=22
age_1=18
age_0>=21 and age_1>=21
#False
2.4.2使用or
只要一个满足,整个通过。
age_0>=21 or age_1>=21
#True
2.5是否包含在列表中(in)
requested_toppings=['mushrooms','onions','pineapple']
'mushrooms' in requested_toppings
#True
'pepperoni' in requested_toppings
#False
2.6是否不包含在列表中(not in)
banned_users=['andrew','carolina','david']
user='marie'
if user not in banned_users:
print(user.title()+", you can post a responce if you wish.")
#Marie, you can post a responce if you wish.
2.7布尔表达式
条件测试的别名,结果要么是True,要么是False.
3. if语句
3.1简单的if语句
age=19
if age>=18:
print("You are old enough to vote!")
print("Have you registered to vote yet?")
#You are old enough to vote!
#Have you registered to vote yet?
3.2if-else语句
条件测试通过时执行一个操作,并没有通过时执行另一个操作。
age=17
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.")
#Sorry, you are too young to vote.
#Please register to vote as soon as you turn 18.
3.3if-elif-else语句
需要检查超过两个的情形,可以使用Python提供的if-elif-else结构。Python只执行其中一个代码块,执行紧跟在它后面的代码,并跳过余下的测试。
age=12
if age<4:
price=0
elif age<18:
price=5
else:
price=10
print("You admission cost is $"+str(price)+".")
#You admission cost is $5.
3.4多elif代码块
可根据需要使用任意数量的elif代码块。
age=66
if age<4:
price=0
elif age<18:
price=5
elif age<65:
price=10
else:
price=5
print("You admission cost is $"+str(price)+".")
#You admission cost is $5.
3.5省略else代码块
并不要求if-elif结构后面必须有else代码块,可用elif代替。
age=66
if age<4:
price=0
elif age<18:
price=5
elif age<65:
price=10
elif age>=65:
price=5
print("You admission cost is $"+str(price)+".")
#You admission cost is $5.
3.6测试多个条件
if-elif-else结构仅适合只有一个条件满足的情况,遇到通过的测试后,Python就跳过余下的测试。
如果必须检查所有条件,应使用一系列if语句。
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("\nFinish making your pizza!")
结果
Adding mushrooms.
Adding extra cheese.
Finish making your pizza!
4.使用if语句处理列表
结合使用if语句和列表。
4.1检查特殊元素
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 pepper right now.")
else:
print("Adding "+requested_topping+".")
print("\nFinished making your pizza.")
#结果
Adding mushrooms.
Sorry, we are out of green pepper right now.
Adding extra cheese.
Finished making your pizza.
4.2确定列表不是空的
在运行for循环前,确定列表是否为空很重要。
在if语句中将列表名用在条件表达式中,Python将在列表至少包含一个元素时返回True,并在列表为空时返回False.
requested_toppings=[]
if requested_toppings:
for requested_topping in requested_toppings:
print("Adding "+requested_topping+".")
print("\nFinished making your pizza.")
else:
print("Are you srue you want a plain pizza?")
#Are you srue you want a plain pizza?
4.3使用多个列表
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.")
#结果
Adding mushrooms.
Sorry, we don't havefrench fries.
Adding extra cheese.
Finished making your pizza.