python从入门到实践-5章if语句

本文通过两个示例详细介绍了Python中if-else、if-elif-else语句的使用,以及如何检查列表是否为空,并针对不同情况执行相应操作。示例包括汽车品牌名的打印和比萨配料的添加过程。

#!/user/bin/env python

cars = ['audi','bmw','subaru','toyota']
for car in cars:
if car == 'bmw':
print(car.upper())
else:
print(car.title())

# == !=
# <= >=
# and or
# in not in
# True False
# if-else
# if-elif-elif-else # 选择一个执行
requesed_toppings = ['mushrooms','green peppers','extra cheese']
for requesed_topping in requesed_toppings:
if requesed_topping == 'green peppers':
print("Sorry,we are out of green peppers now.")
else:
print("Addin" + requesed_topping + ".")
print('\nFinished making your pizza!')


# 确定列表不是空
requesed_toppings = []
if requesed_toppings:
for requesed_topping in requesed_toppings:
print("Adding" + requesed_topping + ".")
print("\nFinished making your pizza!")
else:
print("Are you sure you want a plain pizza?")

转载于:https://www.cnblogs.com/vwei/p/9879982.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值