標籤:style 條件 條件測試 ota als class 結構 str 分享圖片
一、if語句的案例
上面代碼,輸出結果是: Audi BMW Subaru Toyota
二、條件測試(條件運算式,格式如)
每條if語句的核心都是一個值為True或False的運算式,這種運算式被稱為條件測試。 Python
根據條件測試的值為True還是False來決定是否執行if語句中的代碼。如果條件測試的值為True,
Python就執行緊跟在if語句後面的代碼;如果為False, Python就忽略這些代碼。
1、條件檢查的符號:== :相等,!=:不等,>:大於, >=:大於等於,<=:小於等於,<:小於
此處忽略,無需再述。
2、關鍵字and:多條件與門
3、關鍵字or:多條件或門
4、關鍵字in:檢查是否包含某個元素
5、關鍵字not in:檢查是否不包含某個元素
與in的用法是一樣的
6、布林運算式(boolearn資料類型)
boolearn類型的變數只有兩個選擇,True 和False
代碼:
value = True
if value ==False :
print(22)
elif value ==True:
print(11)
else:
print(00)
結果:11
注意:if語句的一般性結構,“if ---elif---else---”,“if ---elif---”,“if ---else---”,“if ---”。
7、if語句的利用
available_toppings = [‘mushrooms‘, ‘olives‘, ‘green peppers‘,
‘pepperoni‘, ‘pineapple‘, ‘extra cheese‘]
requested_toppings = [‘mushrooms‘, ‘french fries‘, ‘extra cheese‘]
for requested_topping in requested_toppings:
if requested_topping in available_toppings:
print("Adding " + requested_topping + ".")
else:
print("Sorry, we don‘t have " + requested_topping + ".")
print("\nFinished making your pizza!")
輸出結果:
Adding mushrooms.
Sorry, we don‘t have french fries.
Adding extra cheese.
Finished making your pizza!
Python學習之路day01——if語句