Python語言中的if語句詳情
1.簡單介紹
每條if語句的核心都是一個(gè)值為True或False的表達(dá)式,這種表達(dá)式被稱為條件測試。Python 根據(jù)條件測試的值為True還是False來決定是否執(zhí)行if語句中的代碼。如果條件測試的值為True,Python就執(zhí)行緊跟在if語句后面的代碼;如果為False,Python就忽略這些代碼。
要判斷是否相等,我們可以使用==來進(jìn)行判斷:
car = 'Audi' car.lower() == 'audi'
輸出的結(jié)果為:
true
比如說我們在測試用戶的用戶名是否與他人重合的時(shí)候我們可以使用到這個(gè)判斷。
要判斷兩個(gè)值是否不等,可結(jié)合使用驚嘆號(hào)和等號(hào)(!=),其中的驚嘆號(hào)表示不,在很多編程語言中都如此:
requested_topping = 'mushrooms'
if requested_topping != 'anchovies':
? print("Hold the anchovies!")輸出的結(jié)果為:
Hold the anchovies!
如果需要對(duì)多個(gè)條件進(jìn)行比較,則可以使用and和or兩個(gè)符號(hào):
num1 = 15
num2 = 20
?
num3 = 25
num4 = 30
?
if num1 == 15 and num2 == 20:
? print("All Right")
?
if num3 == 25 or num4 == 40:
? print("One of them is right")and需要多個(gè)條件同時(shí)成立才能夠成立,而or只需要一個(gè)條件成立就能夠成立。
2.if-else語句
最簡單的if語句只有一個(gè)測試和一個(gè)操作,但是使用了if-else語句之后便可以有兩個(gè)操作:
num = 50
?
if num < 60:
? print("不及格")
else:
? print("及格了")輸出的結(jié)果為:
不及格
if-else語句可以演變?yōu)閕f-elif-else語句,用來執(zhí)行2個(gè)以上的條件判斷對(duì)執(zhí)行對(duì)應(yīng)的操作:
num = 85
?
if num < 60:
? print("不及格")
elif 60<=num and num<=80:
? print("及格")
else:
? print("優(yōu)秀")運(yùn)行的結(jié)果為:
優(yōu)秀
3.用if語句來處理列表
我們可以把if語句和列表相結(jié)合:
food_list = ['apple', 'banana','orange']
?
for food in food_list:
? if food == 'apple':
? ? print("Apple is here")
? elif food == 'bana':
? ? print("Banana is here")
? else:
? ? print("Orange is here")輸出的結(jié)果為:
Apple is here
Orange is here
Orange is here
或者我們可以用來檢測列表是否為空:
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?")運(yùn)行結(jié)果為:
Are you sure you want a plain pizza?
Python語言會(huì)在列表至少包含一個(gè)元素的時(shí)候返回True,而列表為空的是否返回False。
當(dāng)我們有著多個(gè)列表的時(shí)候,我們可以:
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!")行結(jié)果為:
Adding mushrooms.
Finished making your pizza!
Sorry, we don't have french fries.
Finished making your pizza!
Adding extra cheese.
Finished making your pizza!
到此這篇關(guān)于Python語言中的if語句詳情的文章就介紹到這了,更多相關(guān)Python語言中的if語句內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
對(duì)python3 urllib包與http包的使用詳解
今天小編就為大家分享一篇對(duì)python3 urllib包與http包的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05
python對(duì)excel文檔去重及求和的實(shí)例
下面小編就為大家分享一篇python對(duì)excel文檔去重及求和的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04
初學(xué)者學(xué)習(xí)Python好還是Java好
在本篇文章里小編給大家分享的是關(guān)于初學(xué)者學(xué)習(xí)Python好還是Java好的相關(guān)內(nèi)容,需要的朋友們可以學(xué)習(xí)下。2020-05-05
python整小時(shí) 整天時(shí)間戳獲取算法示例
今天小編就為大家分享一篇python整小時(shí) 整天時(shí)間戳獲取算法示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-02-02
在macOS上搭建python環(huán)境的實(shí)現(xiàn)方法
今天小編就為大家分享一篇在macOS上搭建python環(huán)境的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08
python可視化plotly?圖例(legend)設(shè)置
這篇文章主要介紹了python可視化plotly?圖例(legend)設(shè)置,主要介紹了關(guān)于python?的legend圖例,參數(shù)使用說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,需要的朋友可以參考下賣你具體內(nèi)容2022-02-02

