Python编程:从入门到实践-第5章if语句

5.if语句

5.1条件测试

if语句的核心是一个值为True或False的表达式,这种表达式则称为条件测试。Python根据条件测试的值为True还是False来决定是否执行if语句中的代码。如果测试结果为True则执行紧跟在if语句后的代码,如果测试结果为False则忽略这些代码。

  • 一些比较符号:
 含义  符号 
等于==
不等于!=
小于等于<=
大于等于>=

其余判断符号基本与数学运算符号一致。使用以上符号进行条件比较,示例如下:

car='bmw'
car=='bmw'
car!='bmw'

age=18
age>21
age<=22

输出结果如下:

True
False
False
True

对于更为复杂的情况,可以使用关键字检查多个条件:

  • 使用and检查多个条件:若每个条件都通过则判断为True,只要有一个条件没有通过则判断为False。示例如下:
age_0=18
age_1=21
age_0>=21 and age_1>=21
age_0<=21 and age_1>=21

输出结果为:

False
True
  • 使用or检查多个条件:只要有一个条件通过则判断为True,当所有条件都没有通过时判断为False。示例如下:
age_0=18
age_1=21
age_0>=21 and age_1>=21
age_0>=21 and age_1>=22

输出结果为:

True
False
  • 检查特定值是否包含在列表中,示例如下:
modes=['car','bicycle','subway','bus','train']
'bus'in modes

输出结果为:

True

5.2 if语句

5.2.1简单的if语句

最简单的if语句只有一个测试和一个操作:

if conditional_test:
    do something

示例如下:

age=19
if age>=18:
    print("You are old enough to vote!")

输出结果为:

You are old enough to vote!

5.2.2 if-else语句

用以在条件测试通过时执行一个操作,在没有通过时执行另一个操作。示例如下:

age=17
if age>=18:
    print("You are old enough to vote!")
else:
    print('Sorry,you are too young to vote.')

输出结果为:

Sorry,you are too young to vote.

5.2.3 if-elif-else结构

当需要测试的条件超过两个时,就可以使用if-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.')

输出结果为:

Your admission cost is $25.

5.2.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}.')

输出结果为:

Your admission cost is $25.

5.2.5省略else代码块

Python并不要求if-elif后面必须有else代码块,在有些情况下,可以使用elif语句来代替else代码块。示例如下:

age=12
if age < 4:
    price=0
elif age < 18:
    price=25
elif age <65:
    price=40
elif age >= 65:
    price=20

print(f'Your admission cost is ${price}.')

输出结果为:

Your admission cost is $25.

5.3使用if语句处理列表

5.3.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 peppers right now.')
    else:
        print(f'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!

5.3.2确定列表非空

示例如下:

requested_toppings = []

if requested_toppings:
    for requested_topping in requested_toppings:
        print(f'Adding {requested_topping}.')
    print('\nFinished making your pizza!')
else:
    print('Are you sure you want a plain pizza?')

输出结果为:

Are you sure you want a plain pizza?

5.3.3使用多个列表

示例如下:

available_toppings = ['mushrooms','olives','green peppers','pepperonim','pineapple','extra cheese']
requested_toppings = ['mushrooms','green peppers','extra cheese']
for requested_topping in requested_toppings:
    if requested_topping in available_toppings:
        print(f'Adding {requested_topping}.')
    else:
        print(f"Sorry,we don't have {requested_topping}.")
    
print('\nFinished making your pizza!')

输出结果如下:

Adding mushrooms.
Adding green peppers.
Adding extra cheese.
Finished making your pizza!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值