《Python程式設計:從入門到實踐》筆記。
本章主要講述條件語句if, if-else, if-elif, if-elif-else等結構。
1. 條件測試
包括了“相等”,“不等”,“大於”,“小於”,“大於等於”,“小於等於”,“存在於”,“與或非”等判斷。值得注意的是,Python對大小寫敏感:
>>> car = "Audi"
>>> car == "audi"
False
>>> car.lower() == "audi"
True
>>> car != "audi"
True
>>> age = 19
>>> age < 21
True
>>> age <= 21
True
>>> age >= 21
False
>>> age_0 = 22
>>> age_1 = 18
>>> age_0 >= 21 and age_1 >= 21
False
>>> age_0 >= 21 or age_1 >= 21
True
>>> requested_toppings = ['mushrooms', 'onions', 'pineapple']
>>> 'mushrooms' in requested_toppings
True
>>> 'mushrooms' not in requested_toppings
False
複製程式碼
2. if 語句
2.1 簡單的if語句
# 程式碼:
age = 19
if age >= 18:
print("You are old enough to vote!")
# 結果:
You are old enough to vote!
複製程式碼
2.2 if-else 語句
# 程式碼:
age = 17
if age >= 18:
print("You are old enough to vote!")
print("Have you registered to vote yet?")
else:
print("Sorry, you are too young to vote.")
print("Please register to vote as soon as you turn 18!")
# 結果:
Sorry, you are too young to vote.
Please register to vote as soon as you turn 18!
複製程式碼
2.3 if-elif-else 結構
# 程式碼:
age = 12
if age < 4:
price = 0
elif age < 18:
price = 5
else:
price = 10
print("Your admission cost is $" + str(price) + ".")
# 結果:
Your admission cost is $5.
複製程式碼
還可以根據需要使用任意數量的elif
程式碼塊:
# 程式碼:
age = 12
if age < 4:
price = 0
elif age < 18:
price = 5
elif age < 65:
price = 10
else:
price = 5
print("Your admission cost is $" + str(price) + ".")
# 結果:
Your admission cost is $5.
複製程式碼
其次,Python並不要求if-elif
結構後面必須有else
程式碼塊。else
是一條包羅永珍的語句,只要不滿足前面的條件,其中的程式碼就會執行,這可能會引入無效甚至惡意的資料。所以如果知道最終要測試的條件,應考慮使用一個elif程式碼塊來代替else
程式碼塊,使程式碼更清晰,如下:
# 程式碼:
age = 12
if age < 4:
price = 0
elif age < 18:
price = 5
elif age < 65:
price = 10
elif age >= 65:
price = 5
print("Your admission cost is $" + str(price) + ".")
# 結果:
Your admission cost is $5.
複製程式碼
2.4 測試多個條件
if-elif-else
結構功能強大,但僅適用於只有一個條件滿足的情況,即只要其中一個條件滿足,其餘條件都會被跳過,這保證了程式的高效性。然而有時必須檢查你關心的所有條件,這時則應該使用一系列不包含elif
和else
程式碼塊的簡單if
語句:
# 程式碼:
requested_toppings = ["mushrooms", "extra cheese"]
if "mushrooms" in requested_toppings:
print("Adding mushrooms.")
if "pepperoni" in requested_toppings:
print("Adding pepperoni.")
if "extra cheese" in requested_toppings:
print("Adding extra cheese.")
print("\nFinished making your pizza!")
# 結果:
Adding mushrooms.
Adding extra cheese.
Finished making your pizza!
複製程式碼
總之:如果你只想執行一個程式碼塊,就用if-elif-else
結構;如果要執行多個程式碼塊,就使用一系列獨立的if
語句。
3. 使用if語句處理列表
if
語句常和迴圈結構配合使用。
3.1 檢查特殊元素
# 程式碼:
requested_toppings = ["mushrooms", "extra cheese", "green peppers"]
for requested_topping in requested_toppings:
if requested_topping == "green peppers":
print("Sorry, we are out of green peppers right now.")
else:
print("Adding " + requested_topping + ".")
print("\nFinished making your pizza!")
# 結果:
Adding mushrooms.
Adding extra cheese.
Sorry, we are out of green peppers right now.
Finished making your pizza!
複製程式碼
3.2 確定列表不是空的
到目前為止,對於處理的每個列表都做了一個簡單的假設,即它們非空,然而實際工程中,在遍歷一個列表前需要先判斷該列表是否為空:
# 程式碼:
requested_toppings = []
if requested_toppings:
for requested_topping in requested_toppings:
print("Adding " + requested_topping + ".")
print("\nFinished making your pizza!")
else:
print("Are you sure you want a plain pizza?")
# 結果:
Are you sure you want a plain pizza?
複製程式碼
迎大家關注我的微信公眾號"程式碼港" & 個人網站 www.vpointer.net ~