Python中的if、else、elif語句用法簡明講解
下面我們學(xué)習(xí)if語句,輸入下面的代碼,確保能夠正確運(yùn)行。
people = 20 cats = 30 dogs = 15 if people < cats: print "Too many cats! The world is doomed!" if people > cats: print "Not many cats! The world is saved!" if people < dogs: print "The world is drooled on!" if people > dogs: print "The world is dry!" dogs += 5 if people >= dogs: print "People are greater than or equal to dogs." if people <= dogs: print "People are less than or equal to dogs." if people == dogs: print "People are dogs."
運(yùn)行結(jié)果
root@he-desktop:~/mystuff# python ex29.py
Too many cats! The world is doomed! The world is dry! People are greater than or equal to dogs. People are less than or equal to dogs. People are dogs.
加分練習(xí)
通過上面的練習(xí),我們自己猜測一下if語句的作用,用自己的話回答下面的問題。
1. 你認(rèn)為if對它下面的代碼做了什么?
判斷為True就執(zhí)行它下面的代碼,否則不執(zhí)行。
2. 為什么if下面的代碼要縮進(jìn)4個(gè)空格?
為了表示這些代碼屬于if判斷下包括的代碼。
3. 如果不縮進(jìn)會發(fā)生什么?
會提示一個(gè)縮進(jìn)錯(cuò)誤。
4. 你可以從第27節(jié)中拿一些布爾表達(dá)式來做if判斷嗎?
5. 改變people,dogs,cats變量的值,看看會發(fā)生什么?
答案:
1. if語句下面的代碼是if的一個(gè)分支。就像書里的一個(gè)章節(jié),你選擇了這章就會跳到這里閱讀。這個(gè)if語句就像是說:“如果布爾判斷為True,就執(zhí)行下面的代碼,否則跳過這些代碼”。
2. 用冒號結(jié)束一個(gè)語句就是要告訴python,我要開始一個(gè)新的代碼段了??s進(jìn)4個(gè)空格就是說,這些代碼是包含在這個(gè)代碼段中的,和函數(shù)的使用一樣。
3. 不縮進(jìn)會報(bào)錯(cuò),python規(guī)定冒號后面語句必須有縮進(jìn)。
4. 可以,而且可以是復(fù)雜的語句。
5. 修改變量的值后,判斷語句就會相應(yīng)的變True或者False,然后輸出不同的語句。
比較我的答案和你自己的答案,確保你能理解代碼塊這個(gè)概念,因?yàn)檫@個(gè)對于下面的練習(xí)非常重要。
輸入下面的代碼,運(yùn)行它:
people = 30 cars = 40 buses = 15 if cars > people: print "We should take the cars." elif cars < people: print "We should not take the cars." else: print "We can't dicide." if buses > cars: print "That's too many buses." elif buses < cars: print "Maybe we could take the buses." else: print "We still can't decide." if people > buses: print "Alright, let's just take the buses." else: print "Fine, let's stay home then."
運(yùn)行結(jié)果
root@he-desktop:~/mystuff# python ex30.py
We should take the cars. Maybe we could take the buses. Alright, let's just take the buses.
相關(guān)文章
python使用socket實(shí)現(xiàn)的傳輸demo示例【基于TCP協(xié)議】
這篇文章主要介紹了python使用socket實(shí)現(xiàn)的傳輸demo,結(jié)合實(shí)例形式分析了Python使用socket庫基于TCP協(xié)議實(shí)現(xiàn)的客戶端與服務(wù)器端相關(guān)操作技巧,需要的朋友可以參考下2019-09-09
python中隨機(jī)函數(shù)random用法實(shí)例
這篇文章主要介紹了python中隨機(jī)函數(shù)random用法,實(shí)例分析了random函數(shù)的相關(guān)使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
Python生成任意范圍任意精度的隨機(jī)數(shù)方法
下面小編就為大家分享一篇Python生成任意范圍任意精度的隨機(jī)數(shù)方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
淺談python中常用的8種經(jīng)典數(shù)據(jù)結(jié)構(gòu)
這篇文章主要介紹了python中常用的8種經(jīng)典數(shù)據(jù)結(jié)構(gòu),包括原生數(shù)據(jù)結(jié)構(gòu),NumPy包中的數(shù)據(jù)結(jié)構(gòu),以及Pandas包中的數(shù)據(jù)結(jié)構(gòu),需要的朋友可以參考下2023-03-03
python寫入數(shù)據(jù)到csv或xlsx文件的3種方法
這篇文章主要為大家詳細(xì)介紹了python寫入數(shù)據(jù)到csv或xlsx文件的3種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
Python astype(np.float)函數(shù)使用方法解析
這篇文章主要介紹了Python astype(np.float)函數(shù)使用方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06

