1. if語句
cars = ['audi','bmw','subaru','toyota']
for car in cars:
? ? if car == 'bmw': ?#在python中都是用 : 來進(jìn)入下一級(jí)
? ? ? ? print(car.upper())
? ? else:
? ? ? ? print(car.title())
car1 = 'audi'
print(car1 == 'Audi') ?#python中會(huì)檢查大小寫
requested_topping = "mushroom"
if requested_topping != 'anchovies': #python中的不等于判斷 以及不需要括號(hào)
? ? print("Hold the anchovies")
age_0 = 23
age_1 = 26
print(age_0>22 and age_1>22) #and 需要兩個(gè)條件都滿足 可添加括號(hào)
print(age_0<22 or age_1<22)? #or 只需要滿足其中一個(gè)條件即可 可添加括號(hào)
是否在列表中序臂,使用in
# in list or not
requested_toppings = ['mushroom','onions','pineapple']
a = 'mushroom' in requested_toppings
b = 'pepper' in requested_toppings
print(a) ?#true
print(b) ?#false
food = 'pepper'
if food not in requested_toppings:
print(food.title() + ' should also be added to the list')