摘要:本章主要講述條件語句等結(jié)構(gòu)。是一條包羅萬象的語句,只要不滿足前面的條件,其中的代碼就會(huì)執(zhí)行,這可能會(huì)引入無效甚至惡意的數(shù)據(jù)。使用語句處理列表語句常和循環(huán)結(jié)構(gòu)配合使用。
《Python編程:從入門到實(shí)踐》筆記。1. 條件測(cè)試
本章主要講述條件語句if, if-else, if-elif, if-elif-else等結(jié)構(gòu)。
包括了“相等”,“不等”,“大于”,“小于”,“大于等于”,“小于等于”,“存在于”,“與或非”等判斷。值得注意的是,Python對(duì)大小寫敏感:
>>> 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 False2. if 語句 2.1 簡單的if語句
# 代碼: age = 19 if age >= 18: print("You are old enough to vote!") # 結(jié)果: 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!") # 結(jié)果: Sorry, you are too young to vote. Please register to vote as soon as you turn 18!2.3 if-elif-else 結(jié)構(gòu)
# 代碼: age = 12 if age < 4: price = 0 elif age < 18: price = 5 else: price = 10 print("Your admission cost is $" + str(price) + ".") # 結(jié)果: Your admission cost is $5.
還可以根據(jù)需要使用任意數(shù)量的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) + ".") # 結(jié)果: Your admission cost is $5.
其次,Python并不要求if-elif結(jié)構(gòu)后面必須有else代碼塊。else是一條包羅萬象的語句,只要不滿足前面的條件,其中的代碼就會(huì)執(zhí)行,這可能會(huì)引入無效甚至惡意的數(shù)據(jù)。所以如果知道最終要測(cè)試的條件,應(yīng)考慮使用一個(gè)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) + ".") # 結(jié)果: Your admission cost is $5.2.4 測(cè)試多個(gè)條件
if-elif-else結(jié)構(gòu)功能強(qiáng)大,但僅適用于只有一個(gè)條件滿足的情況,即只要其中一個(gè)條件滿足,其余條件都會(huì)被跳過,這保證了程序的高效性。然而有時(shí)必須檢查你關(guān)心的所有條件,這時(shí)則應(yīng)該使用一系列不包含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(" Finished making your pizza!") # 結(jié)果: Adding mushrooms. Adding extra cheese. Finished making your pizza!
總之:如果你只想執(zhí)行一個(gè)代碼塊,就用if-elif-else結(jié)構(gòu);如果要運(yùn)行多個(gè)代碼塊,就使用一系列獨(dú)立的if語句。
3. 使用if語句處理列表if語句常和循環(huán)結(jié)構(gòu)配合使用。
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(" Finished making your pizza!") # 結(jié)果: Adding mushrooms. Adding extra cheese. Sorry, we are out of green peppers right now. Finished making your pizza!3.2 確定列表不是空的
到目前為止,對(duì)于處理的每個(gè)列表都做了一個(gè)簡單的假設(shè),即它們非空,然而實(shí)際工程中,在遍歷一個(gè)列表前需要先判斷該列表是否為空:
# 代碼: requested_toppings = [] if requested_toppings: for requested_topping in requested_toppings: print("Adding " + requested_topping + ".") print(" Finished making your pizza!") else: print("Are you sure you want a plain pizza?") # 結(jié)果: Are you sure you want a plain pizza?
迎大家關(guān)注我的微信公眾號(hào)"代碼港" & 個(gè)人網(wǎng)站 www.vpointer.net ~
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/41798.html
摘要:本章主要是學(xué)習(xí)的文件操作,主要是從文件中讀取數(shù)據(jù)以及將數(shù)據(jù)存儲(chǔ)到文件中,還有錯(cuò)誤處理,異常類,模塊等。函數(shù)返回一個(gè)文件對(duì)象,用于接收該對(duì)象。異常中使用被稱為異常的特殊對(duì)象來管理程序執(zhí)行期間發(fā)生的錯(cuò)誤。 《Python編程:從入門到實(shí)踐》筆記。本章主要是學(xué)習(xí)Python的文件操作,主要是從文件中讀取數(shù)據(jù)以及將數(shù)據(jù)存儲(chǔ)到文件中,還有錯(cuò)誤處理,異常類,json模塊等。 1. 從文件中讀數(shù)據(jù) ...
摘要:本章主要介紹如何進(jìn)行用戶輸入,循環(huán),以及與循環(huán)配合使用的語句。函數(shù)在中,使用函數(shù)獲取用戶輸入,這里請(qǐng)注意的返回值為字符串。值得提醒的是,編寫循環(huán)時(shí)應(yīng)避免死循環(huán),或者叫做無限循環(huán),比如循環(huán)忘記了變量自增。 《Python編程:從入門到實(shí)踐》筆記。本章主要介紹如何進(jìn)行用戶輸入,while循環(huán),以及與循環(huán)配合使用的break, continue語句。 1. input() 函數(shù) 在Pytho...
摘要:本章主要介紹列表的基礎(chǔ)與簡單操作。列表是什么列表由一系列按特定順序排列的元素組成。中用中括號(hào)來表示列表,并用逗號(hào)分隔其中的元素。代碼結(jié)果打印了該列表的內(nèi)部表示訪問與使用列表中的元素大多數(shù)編程語言中,索引都是從開始的,而不是從開始的。 《Python編程:從入門到實(shí)踐》筆記。本章主要介紹列表的基礎(chǔ)與簡單操作。 1. 列表(List)是什么 列表由一系列按特定順序排列的元素組成。類似于C/...
摘要:也就是說,你可以將上述代碼中的看做單元測(cè)試,而將看做測(cè)試用例。在測(cè)試類中的每一個(gè)測(cè)試方法都必須以開頭,否則將不會(huì)被認(rèn)定是一個(gè)單元測(cè)試。 《Python編程:從入門到實(shí)踐》筆記。本章主要學(xué)習(xí)如何使用Python標(biāo)準(zhǔn)庫中的unittest模塊對(duì)代碼進(jìn)行簡單的測(cè)試。 1. 前言 作為初學(xué)者,并非必須為你嘗試的所有項(xiàng)目編寫測(cè)試;但參與工作量較大的項(xiàng)目時(shí),你應(yīng)對(duì)自己編寫的函數(shù)和類的重要行為進(jìn)行測(cè)...
摘要:也就是說,你可以將上述代碼中的看做單元測(cè)試,而將看做測(cè)試用例。在測(cè)試類中的每一個(gè)測(cè)試方法都必須以開頭,否則將不會(huì)被認(rèn)定是一個(gè)單元測(cè)試。 《Python編程:從入門到實(shí)踐》筆記。本章主要學(xué)習(xí)如何使用Python標(biāo)準(zhǔn)庫中的unittest模塊對(duì)代碼進(jìn)行簡單的測(cè)試。 1. 前言 作為初學(xué)者,并非必須為你嘗試的所有項(xiàng)目編寫測(cè)試;但參與工作量較大的項(xiàng)目時(shí),你應(yīng)對(duì)自己編寫的函數(shù)和類的重要行為進(jìn)行測(cè)...
閱讀 1461·2021-10-08 10:04
閱讀 2797·2021-09-22 15:23
閱讀 2780·2021-09-04 16:40
閱讀 1234·2019-08-29 17:29
閱讀 1560·2019-08-29 17:28
閱讀 3044·2019-08-29 14:02
閱讀 2287·2019-08-29 13:18
閱讀 934·2019-08-23 18:35