时间:2018/04/23
主要内容:5.if语句
if语句
示例
cars=['audi','bmw','subaru','toyota']
for car in cars:
if car=='bmw':
print(car.upper())#注意缩进格式
else:
print(car.title())
条件测试
每条if语句的核心都是一个值为True或者False的表达式,这种表达式称为条件测试。Python会根据条件测试的值为True还是False来决定是否执行if语句中的代码。
检查是否相等
利用‘==’相等运算符来判断两端的值是否相等,想等则返回True,否则返回False。
!在判断字符是否相等时要注意大小写的不同也会被视为不同。
检查是否不相等
判断两个值是否不相等,可以结合惊叹号和等号(!=)。
比较数字
包括等于、不等于、小于、大于、小于等于、大于等于这些数学比较。
检查多个条件
通过‘and’进行合操作,即and两端判断都为True时才会返回True。
通过‘or’进行或操作,即or两端判断有一个为True就会返回True。
检查特定值是否包含在列表中
检查特定值是否不包含在列表中
布尔表达式
布尔表达式是条件测试的别名。
if语句
简单的if语句
!通过缩进来划分if语句和主语句
age = 19
if age >= 18:
print('You are old enough to vote!')
print('Have you registered to vote yet?')#当返回值为True时缩进内的语句才会被解释器解释并运行
if-else语句
age = 17
if age >= 18:
print('You are old enough to vote!')
print('Have you registered to vote yet?')#当返回值为True时缩进内的语句才会被解释器解释并运行
else:
print('Sorry,you are too young to vote.')
print('Please register to vote as soon as you turn 18!')#当返回值为False时该缩进内的语句才会被执行
if-elif-else结构
经常需要检查超过两个的情形,为此可使用Python提供的if-elif-else结构。Python只执行该结构中的一个代码块,依次检测每个条件测试,直到遇到通过了的条件测试。测试通过后,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.')
#为了使代码简洁可以做以下修改
if age < 4:
price=0
elif age < 18:
price=5
else:
price=10
print('Your admission cost is $'+str(price)+'.')
使用多个elif代码块&省略else代码块
#Python并不要求if-elif结构后必须有else代码块。有时候else代码块很有用,可以包括除了elif条件以外其他所有条件,但是某些情况下使用一条elif语句来处理特定的情形会更加清晰避免else引入无效甚至恶意的数据
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)+'.')
测试多个条件
if-elif只会执行一个代码块的代码,如果要运行多个代码块则需要使用一系列独立的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('\nFinished making your pizza!')
如果使用以下if-elif-else结构,代码将不能达到期望,因为有一个测试通过后将会跳过余下的测试:
requested_toppings=['mushrooms','extra cheese']
if 'mushrooms' in requested_toppings:
print('Adding mushrooms.')
elif 'pepperoni'in requested_toppings:
print('Adding pepperoni.')
elif 'extra cheese'in requested_toppings:
print('Adding extra cheese.')
print('\nFinished making your pizza!')
使用if语句处理列表
检查特殊元素
在for循环中加入if语句:
requested_toppings=['mushrooms','extra cheese','green peppers']
for requested_topping in requested_toppings:
if requested_topping=='green peppers':
print('Sorry,we are out out of green peppers right now.')
else:
print('Adding '+requested_topping+'.')
print('\nFinished making your pizza!')
确定列表不是空的
requested_toppings=[]
if requested_toppings:#if语句中将列表名用在条件表达式中,Pyhton将在列表至少包含一个元素时返回True,并在列表为空时返回False。
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?')
使用多个列表
#提供食材列表和需求食材列表的调用
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!')
设置if语句的格式
养成良好的书写习惯。