2017.5.4 if_1

1. if語句

cars = ['audi','bmw','subaru','toyota']

for car in cars:

? ? if car == 'bmw': ?#在python中都是用 : 來進入下一級

? ? ? ? print(car.upper())

? ? else:

? ? ? ? print(car.title())


car1 = 'audi'

print(car1 == 'Audi') ?#python中會檢查大小寫

requested_topping = "mushroom"

if requested_topping != 'anchovies': #python中的不等于判斷 以及不需要括號

? ? print("Hold the anchovies")

age_0 = 23

age_1 = 26

print(age_0>22 and age_1>22) #and 需要兩個條件都滿足 可添加括號

print(age_0<22 or age_1<22)? #or 只需要滿足其中一個條件即可 可添加括號

是否在列表中,使用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')

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容