【精品分享】《Python編程從入門到實踐》學習筆記——第五章 if語句

本系列記錄的是自己學習筆記,以及一些總結,並非照搬書本,道友可進行查漏補缺,與君共勉。

本系列文章傳送門
第2章 變量和簡單數據類型
第3章 列表簡介
第4章 操作列表
第5章 if語句
第6章 字典          未完待續
第7章 while循環 未完待續
第8章 函數         未完待續
第9章 類            未完待續
第10章 文件和異常 未完待續
項目實戰



5.1 條件測試

每條if語句的核心都是一個值爲True或False的表達式,這種表達式被稱爲條件測試

5.1.1 檢查是否相等(==)

雙等號兩邊的值相等,返回True;不等則返回False.

# 例1
car = 'bmw'
car == 'bmw'
# 輸出結果
True
===============================
# 例2
car = 'audi'
car == 'bmw'
# 輸出結果
False

特別解析:

  1. 一個等號(=),表示賦值,解讀爲“將變量car的值設置爲audi”;
  2. 兩個等號(==),表示判斷,檢查兩邊值是否相等;
  3. 雙等號判斷兩邊是否相等時嚴格區分大小寫;
# 大小寫不同將返回False
car = 'Audi'
car == 'audi'
# 輸出結果
False
  1. 若只想檢查變量的值,不考慮大小寫,可將變量的值先轉換爲小寫,再進行比較。
car = 'Audi'
car.lower() == 'audi'
# 輸出結果
True

5.1.2 檢查是否不相等(!=)

answer = 17
if answer != 42:
    print('That is not the correct answer, please try again!')

特別解析
數字的比較,還有大於 > ,大於等於 >= ,小於 < ,小於不等於 <=

5.1.3 檢查多個條件

(一)且的關係用and
多個條件同時滿足才返回True!

age = 20
sex = 'man'
if age >= 18 and sex == 'man':
    print("You're a grown man.")
    
# 輸出結果
You're a grown man.

(二)或的關係用or
多個條件只要有一個滿足即返回True!

phone = 'huawei'
if phone == 'huawei' or phone == 'xiaomi':
    print("This is a Mobile phone.")
    
# 輸出結果
This is a Mobile phone.

5.1.4 檢查特定值在列表中(in)、不在列表中(not in)

# 在列表中返回True
requested_toppings = ['mushrooms', 'onions', 'pineapple']
'mushrooms' in requested_toppings
# 輸出結果
True

# 不在列表中返回True
'pepperoni' in requested_toppings
# 輸出結果
False
banned_users = ['andrew', 'carolina', 'david']
user = 'marie'
if user not in banned_users:
    print(user.title() + ", you can post a response if you wish.")
# 輸出結果
Marie, you can post a response if you wish.

5.2 if語句

很簡單,感覺沒太多好說的,就舉例說明各種if表達式吧。

5.2.1 簡單的if語句

注意if後要有冒號, 後面的代碼塊要縮進

age = 19
if age >= 18:
    print("You are old enough to vote!")

# 輸出結果
You are old enough to vote!

5.2.2 if-else 語句

注意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!

5.2.3 if-elif-else語句

注意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.

5.2.4 多個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.

5.2.5 省略else代碼塊

else是一條包羅萬象的語句,只要不滿足任何if或elif中的條件測試,其中的代碼就會執行,這可能會引入無效甚至惡意的數據。
如果知道最終要測試的條件,應考慮使用一個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.

5.2.6 測試多個條件

if-elif-else結構功能強大,但僅適合用於只有一個條件滿足的情況。有時候我們需要檢查多個條件,每個條件爲True時都採取相應措施。
這時就使用多個獨立的簡單if語句(不包含elif和else).

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語句。


如文章對您有幫助,感謝您的點贊+關注(^ _ ^)

福利:添加關注、評論區留言免費贈送自己學習資料

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章