1. 程式人生 > >pathon筆記——第5章 if語句

pathon筆記——第5章 if語句

1、條件測試
(1)檢查多個條件
   1)使用 and 檢查多個條件 2)使用 or 檢查多個條件
      注意:pathon裡沒有&&和||運算子,取而代之的是and和or
(2)檢查特定值是否包含在列表中——使用關鍵字 in 
例:
>>> requested_toppings = ['mushrooms', 'onions', 'pineapple']
>>> 'mushrooms' in requested_toppings
True
>>> 'pepperoni' in requested_toppings
False
(3)檢查特定值是否不包含在列表中——使用關鍵字 not in
例:
banned_users = ['andrew', 'carolina', 'david']
user = 'marie'
if user not in banned_users:
    print(user.title() + ", you can post a response if you wish.")
(4)布林表示式
布林表示式的結果要麼為 True ,要麼為 False 。   布林值通常用於記錄條件。

2、if 語句
(1)簡單if語句
例:
age = 19
if age >= 18:
    print("You are old enough to vote!")
    print("Have you registered to vote yet?")
如果測試通過了,將執行 if 語句後面所有縮排的程式碼行,否則將忽略它們。
在緊跟在 if 語句後面的程式碼塊中,可根據需要包含任意數量的程式碼行。
(2) if-else 語句
如果需要在條件測試通過了時執行一個操作,並在沒有通過時執行另一個操作;在這種情況下,可使用 Python 提供的 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!")
(3)if-elif-else 結構
如果需要檢查超過兩個的情形,為此可使用 Python 提供的 if-elif-else 結構
例:
age = 12
if age < 4:
    print("Your admission cost is $0.")
elif age < 18:
    print("Your admission cost is $5.")
else:
    print("Your admission cost is $10.")
注:1)根據需要使用任意數量的 elif 程式碼塊。
    2)if-elif 結構後面可以沒有 else 程式碼塊。
(4)測試多個條件
在這種情況下,應使用一系列不包含 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!")
注意:
如果像下面這樣轉而使用 if-elif-else 結構,程式碼將不能正確地執行,因為有一個測試通過後,就會跳過餘下的測試:
requested_toppings = ['mushrooms', 'extra cheese']
if 'mushrooms' in requested_toppings:
print("Adding mushrooms.")
elif 'pepperoni' in requested_toppings:
print("Adding pepperoni.")
elif 'extra cheese' in requested_toppings:
print("Adding extra cheese.")
print("\nFinished making your pizza!")

3、使用if語句處理列表
(1)檢查特殊元素
檢查列表中的特殊值,並對其做合適的處理。
例:
requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
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!")
解釋:
如果比薩店的青椒用完了,這裡在比薩中新增每種配料前都進行檢查。
if處的程式碼檢查顧客點的是否是青椒,如果是,就顯示一條訊息,指出不能點青椒的原因。
else處的程式碼確保其他配料都將新增到比薩中。
(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?")
解釋:
在製作比薩前檢查顧客點的配料列表是否為空。如果列表是空的,就向顧客確認他是否要點普通比薩;如果列表不為空,就像前面的示例那樣製作比薩。
(3)使用多個列表
例:
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!")

5、if 語句的格式
PEP 8 提供的唯一建議是,在諸如 == 、 >= 和 <= 等比較運算子兩邊各新增一個空格,例如, if age < 4: 要比 if age<4: 好。
這樣的空格不會影響 Python 對程式碼的解讀,而只是讓程式碼閱讀起來更容易。