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 + ".")

猜你喜欢

转载自blog.csdn.net/mugong11/article/details/81188257