python学习之路(四)-IF语句

学习来源于《Python编程:从入门到实践》,感谢本书的作者和翻译工作者。

下面为学习笔记

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

age1=20
age2=30
if age1 >=18 and age2 >=18:#or 是 或
    print("YES")
else:
    print("NO")
# 关键字in让Python检查列表requested_toppings是否包含 'mushrooms'
requested_toppings = ['mushrooms', 'onions', 'pineapple']
if 'mushrooms' in requested_toppings:
    print("YESa")
# 还有些时候,确定特定的值未包含在列表中很重要;在这种情况下,可使用关键字not in。
if 'MOMO' not  in requested_toppings:
    print("YESb")
ageH=22
if ageH <=2:
    print("He is a baby.")
elif ageH<=18:
    print("He is a teen.")
elif ageH<=40:
    print("He is a middle.")
else:
    print("He is an old man.")
# 在这里,我们首先创建了一个空列表,其中不包含任何配料(见)。在处我们进行了简
# 单检查,而不是直接执行for循环。在if语句中将列表名用在条件表达式中时, Python将在列表
# 至少包含一个元素时返回True,并在列表为空时返回False。requested_toppings = []
requested_toppings1 = []
if requested_toppings1:
    for requested_topping1 in requested_toppings1:
        print("Adding " + requested_topping1 + ".")
        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_toppings2 = ['mushrooms', 'french fries', 'extra cheese']
for requested_topping2 in requested_toppings2:
    if requested_topping2 in available_toppings:
        print("Adding " + requested_topping2 + ".")
    else:
        print("Sorry, we don't have " + requested_topping2 + ".")

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值