python学习(五)if语句
条件测试
- 每条if语句的核心都是一个值为True或False的表达式,这种表达式被称之为条件测试。
检查是否相同
>>> car = 'bmw'
>>> car == 'bmw'
True
>>> car = 'audi'
>>> car == 'bmw'
False
检查是否相同时不考虑大小写
- python检查是否相同时区分大小写的。
>>> car = 'Audi'
>>> car == 'audi'
False
- 只想检查变量的值,可以将变量的值转换成小写,再进行比较
>>> car = 'Audi'
>>> car.lower() == 'audi'
True
检查是否不相等
- !=可以判断两个值是否不相等
requested_topping = 'mushrooms'
if requested_topping != 'anchovies':
print("hold the anchovies")
hold the anchovies
比较数字
- 检查数值
answer = 17
if answer != 42:
print("That is not the correct answer,Please try again")
That is not the correct answer,Please try again
- 条件语句包括各种数学比较,如小于、小于等于、大于、大于等于等
True
>>> age = 19
>>> age < 20
True
>>> age <= 20
True
>>> age > 20
False
>>> age >= 20
False
检查多个条件
- 使用关键字and或or
>>> age_0 = 22
>>> age_1 = 18
>>> age_0 >= 21 and age_1 >= 21
False
>>> age_0 >= 21 or age_1 <= 21
True
检查特定值是否包含在列表中
- 使用关键字in
>>> requested_topping = ['mushrooms','onions','pineapples']
>>> 'mushrooms' in requested_topping
True
>>> 'pepperroni' in requested_topping
False
检查特定值是否不包含在列表中
- 使用关键字not in
banned_users =['andrew','carolina','davia']
user = "marie"
if user not in banned_users:
print(user.title() + ", you can post a reponse if you wish")
Marie, you can post a reponse if you wish
布尔表达式
条件测试的别名,布尔表达式的结果要么为True,要么为False
布尔值通常用户纪律条件,如游戏是否在运行。
>>> game_active = True
>>> can_edit = False
if语句
简单的if语句
age = 19
if age >= 18:
print("You are old enough to vote")
You are old enough to vote
if-else语句
- 条件测试通过时执行一个操作,没有通过执行另一个操作。
age = 17
if age >= 18:
print("You are old enough to vote")
else:
print("Sorry,you are too young to vote.")
if-elif-else结构
- 条件测试超过两种情形时:python提供if-elif-else结构。
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")
Your admission cost is $5
使用多个elif代码块
- 可根据需求使用任意数量的elif代码块
age = 12
if age < 4:
price = 0
elif age < 18:
price = 5
elif age < 65:
price = 10
else:
price = 5
print("Your admission cost is $" + str(price) + ".")
Your admission cost is $5.
省略else代码块
- 在有些情况下else代码块很有用;而其他情况下,使用一条elif语句处理特定的情形更清晰。
age = 88
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) + ".")
Your admission cost is $5.
测试多个条件
- 每条条件都为True时采取相应措施,适合这种方法。
requested_topping = ['mushrooms','onions','pineapples']
if 'mushrooms' in requested_topping:
print("Adding mushrooms")
if 'onions' in requested_topping:
print("Adding onions")
if 'pineapples' in requested_topping:
print('Adding pineapples')
print("\nFinished making your pizza")
Adding mushrooms
Adding onions
Adding pineapples
Finished making your pizza
使用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")
Adding mushrooms.
Sorry,we are out of green peppers right now
Adding extra cheese.
Finished making your pizza
确定列表不是空的
- if语句将列表名用于条件表达式中,列表至少包含一个元素时返回True,列表为空时返回False
使用多个列表
- 多个列表,进行添加判断
vailable_toppings = ['mushrooms','olives','green peppers','pepperoni','pineapple','extra cheese']
requested_toppings = ['mushrooms','grench fries','extra cheese']
for requested_topping in requested_toppings:
if requested_topping in available_toppings:
print("Adding " + requested_topping + ".")
else:
print("Sorry, wo don't have " + requested_topping + ".")
Adding mushrooms.
Sorry, wo don't have grench fries.
Adding extra cheese.