python中if及if-else如何使用
if 結(jié)構(gòu)
if 結(jié)構(gòu)允許程序做出選擇,并根據(jù)不同的情況執(zhí)行不同的操作
基本用法
比較運(yùn)算符
根據(jù) PEP 8 標(biāo)準(zhǔn),比較運(yùn)算符兩側(cè)應(yīng)該各有一個空格,比如:5 == 3。 PEP8 標(biāo)準(zhǔn)
==(相等):如果該運(yùn)算符兩側(cè)的值完全相同則返回 True
!=(不等):與相等相反
print(5 == '5') print(True == '1') print(True == 1) print('Eric'.lower() == 'eric'.lower())
>(大于):左側(cè)大于右側(cè)則輸出 True
<(小于):與大于相反
>=(大于等于):左側(cè)大于或者等于右側(cè)則輸出 True
<=(小于等于):左側(cè)小于或者等于右側(cè)則輸出 True
print(5 > 3) print(2 > True) print(True > False)
if的用法
1.只有 if 進(jìn)行判斷
desserts = ['ice cream', 'chocolate', 'apple crisp', 'cookies'] favorite_dessert = 'apple crisp' hate_dessert = 'chocolate' for dessert in desserts: if dessert == favorite_dessert: print("%s is my favorite dessert!" % dessert.title())
2. if - else 進(jìn)行判斷
for dessert in desserts: # 比較運(yùn)算符(== 相等 、!= 不等、> 大于、>= 大于等于、< 小于、<=小于等于) if dessert == favorite_dessert: print("%s is my favorite dessert!" % dessert.title()) # elif => else + if 當(dāng)前值不符合上面 if 的判斷條件,執(zhí)行 elif 的判斷條件 else: print("I like %s." % dessert)
3. if - elif - else 進(jìn)行判斷,其中 elif 不是唯一的,可以根據(jù)需要添加,實現(xiàn)更細(xì)粒度的判斷
# 對不同的 dessert 輸出不完全相同的結(jié)果 for dessert in desserts: # 比較運(yùn)算符(== 相等 、!= 不等、> 大于、>= 大于等于、< 小于、<=小于等于) if dessert == favorite_dessert: print("%s is my favorite dessert!" % dessert.title()) # elif => else + if 當(dāng)前值不符合上面 if 的判斷條件,執(zhí)行 elif 的判斷條件 elif dessert == hate_dessert: print("I hate %s." % dessert) # 當(dāng)前值不符合上面所有的判斷條件,就執(zhí)行 else 里的語句 # 當(dāng)然如果這個else 不需要的話,可以不寫 else: print("I like %s." % dessert)
值得注意的一點是:當(dāng)整個 if 判斷滿足某一個判斷條件時,就不會再繼續(xù)判斷該判斷條件之后的判斷
4.特殊的判斷條件
if 0: # 其他數(shù)字都返回 True print("True.") else: print("False.") # 結(jié)果是這個 if '': #其他的字符串,包括空格都返回 True print("True.") else: print("False.") # 結(jié)果是這個 if None: # None 是 Python 中特殊的對象 print("True.") else: print("False.") # 結(jié)果是這個 if 1: print("True.") # 結(jié)果是這個 else: print("False.")
實例擴(kuò)展:
實例(Python 3.0+)實例一:
# Filename : test.py # author by : www.runoob.com # 用戶輸入數(shù)字 num = float(input("輸入一個數(shù)字: ")) if num > 0: print("正數(shù)") elif num == 0: print("零") else: print("負(fù)數(shù)")
實例(Python 3.0+)實例二:
# Filename :test.py # author by : www.runoob.com # 內(nèi)嵌 if 語句 num = float(input("輸入一個數(shù)字: ")) if num >= 0: if num == 0: print("零") else: print("正數(shù)") else: print("負(fù)數(shù)")
到此這篇關(guān)于python中if及if-else如何使用的文章就介紹到這了,更多相關(guān)python中條件語句總結(jié)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python密碼學(xué)換位密碼及換位解密轉(zhuǎn)置加密教程
這篇文章主要為大家介紹了python密碼學(xué)換位密碼及換位解密轉(zhuǎn)置加密教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Python實現(xiàn)藍(lán)線挑戰(zhàn)特效的示例代碼
在抖音曾經(jīng)火了一陣子的藍(lán)線挑戰(zhàn)特效,其原理很簡單。本文將試著用opencv-python實現(xiàn)這個效果,做了攝像頭版本和視頻處理版本,感興趣的可以學(xué)習(xí)一下2022-10-10Python?中的?Counter?模塊及使用詳解(搞定重復(fù)計數(shù))
Counter 是一個簡單的計數(shù)器,用于統(tǒng)計某些可哈希對象的數(shù)量。它以字典的形式存儲元素和它們的計數(shù),這篇文章主要介紹了Python?中的?Counter?模塊及使用詳解(搞定重復(fù)計數(shù)),需要的朋友可以參考下2023-04-04淺談python3發(fā)送post請求參數(shù)為空的情況
今天小編就為大家分享一篇淺談python3發(fā)送post請求參數(shù)為空的情況,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12Python數(shù)據(jù)結(jié)構(gòu)dict常用操作代碼實例
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)dict常用操作代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03