第五章 if语句

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


# 5.1 简单示例
cars=['audi','bmw','subaru','toyota']
for car in cars:
    if car== 'bmw':
        print(Ture)
    else:
        print(False)

5.2 条件测试

每条if语句地核心都是一个值为True或False的表达式,这种表达式被称为条件测试。python根据条件测试的值为true还是false来决定是否执行if语句中的代码。如果条件测试的值为true,python就执行紧跟在if语句后面的代码。如果为false,就忽略这些代码。

5.2.1 检查是否相等

car='bmw'
car=='bmw'

5.2.2 检查是否相等时不考虑大小写

在python中检查是否相等时区分大小写

5.2.3 检查是否不相等

要判断两个值是否不等,可使用!=

requested_topping='mushrooms'
if requested_topping!='anchovies':
    print('Hold the anchovies')

5.2.4 比较数字

age=8
if age!= 18:
    print('get out')

5.2.5 检查多个条件

  1. 使用and检查多个条件

将两个条件测试合二为一

age_0=18
age_1=22
if age_0>=18 and age_1<=23:
    print('yes')
  1. 使用or检查多个条件
    至少一个满足就可以通过测试

5.2.6 检查特定值是否包含在列表中

使用关键字in

requested_toppings=['mushrooms','onions','pineapple']
'mushrooms' in requested_toppings

5.2.7 检查特定值是否不包含在列表中

使用关键字not in

5.2.8 布尔表达式

条件测试的别名,与条件表达式一样,布尔表达式的结果要么为True,要么为False

game_active=True
can_edit=False

5.3 if语句

5.3.1 简单的if语句

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

在if语句中,缩进的作用与for循环中相同。如果测试通过了,将执行if语句后面所有缩进的代码行,否则忽略。

age=19
if age>=18:
    print('You are old enough to vote')
    print('Have you registered to vote yet?')

5.3.2 if-else语句

在条件通过时执行一个操作,没通过执行另一个操作。

age=17
if age >=18:
    print('yes')
else:
    print("nope"

5.3.3 if-else-if结构(多个的情况)

age=12
if age <4:
    print('free')
elif age<18:
    print("5")
else:
    print(10)

5.3.5 省略else代码块

age=12
if age<4:
    price=0
elif age<18 or age>=65:
    price=5
elif age<65:
    price=10
print(f"Your admission cost is {str(price)}")

5.3.6 测试多个条件

if-elif-else 仅适合用于只有一个条件满足的情况,但有时候必须检查所有条件。
可能有多个条件为True,且你需要在每个条件为True时都采取相应措施

requested_toppings=['mushrooms','extra cheese']
if 'mushrooms' in requested_toppings:
    print('Adding mushrooms')
if 'extra cheese' in requested_toppings:
    print('Adding extra cheese')

总之,如果只想执行一个代码块,就是用if-elif-else结构,如果要运行多个代码块,就使用一系列独立的if语句。

动手试一试

5-3

alien_color=['green','red']
if  'green'in alien_color:
    print('5 points')

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('\nFinished making your pizza')

如果青椒用完了怎么办?

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')

for中包含一条if语句。

5.4.2 确定列表不是空的

在for循环前确定列表是否为空很重要

requested_toppings=[]#空列表,不包含任何配料
if requested_toppings:# 2处
    for requested_topping in requested_toppings:
        print(f"Adding {requested_topping}")
else:
    print('Are you sure you want a plane pizza')

在2处进行了简单的检查,而不是直接执行for循环。在if语句中将列表名用在条件表达式中时,python将在列表至少包含一个元素时返回true,并在列表为空时返回false

5.4.3 使用多个列表

定义两个列表,其中第一个列表包含比萨店供应的配料,而第二个列表包含顾客点的配料。

available_toppings=['mushrooms','olivers','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('add')
    else:
        print('sorry')

小结

  • 如何编写结果要么为True要么为False的条件测试
  • 学习了编写if语句,if-elif,if-else。
  • 如何在利用高效的for循环的同时,以不同于其他元素的方式对特定的列表元素进行处理,
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值