python空值判斷方式(if xxx和if xxx is None的區(qū)別及說(shuō)明)
if xxx 和if xxx is None的區(qū)別
一、 if xxx
None,’’,0,[],{},() ,False都被判斷為空值(not xxx等價(jià))
如下代碼輸出所示,
if __name__ == '__main__':
? ? print("---not None == (not '') == (not 0) == (not []) == (not {}) == (not ()) == (not False)---")
? ? print(not None == (not '') == (not 0) == (not []) == (not {}) == (not ()) == (not False))輸出
---not None == (not '') == (not 0) == (not []) == (not {}) == (not ()) == (not False)---
True
if xxx
如下代碼輸出所示,
if __name__ == '__main__':
? ? print("---output a,b---")
? ? a = []
? ? b = None
? ? print("a=[]")
? ? print("b=None")
? ? print("--- if x")
? ? if a:
? ? ? ? print("a")
? ? else:
? ? ? ? print("None")
? ? if b:
? ? ? ? print("b")
? ? else:
? ? ? ? print("None")輸出
---output a,b---
a=[]
b=None
--- if x
None
None
結(jié)論:
將空列表?yè)Q成上述的其他空類型,結(jié)果一樣。
如果需要過(guò)濾None值和空對(duì)象時(shí)(如[],{},''等),可使用這種寫(xiě)法
二、 if xxx is None
該寫(xiě)法可將None和其他空值對(duì)象區(qū)分開(kāi)來(lái)
如下代碼輸出所示:
if __name__ == '__main__':
? ? a = []
? ? b = None
? ? print("a=[]")
? ? print("b=None")
? ? print("--- is None")
? ? if a is None:
? ? ? ? print("None")
? ? else:
? ? ? ? print("a")
? ? if b is None:
? ? ? ? print("None")
? ? else:
? ? ? ? print("b")輸出
---output a,b---
a=[]
b=None
--- is None
a
None
結(jié)論:
需要區(qū)分[],{},'',()等空值對(duì)象與None的區(qū)別時(shí)時(shí)可使用這種寫(xiě)法
貼下簡(jiǎn)單的測(cè)試代碼
if __name__ == '__main__':
? ? print("---not None == (not '') == (not 0) == (not []) == (not {}) == (not ()) == (not False)---")
? ? print(not None == (not '') == (not 0) == (not []) == (not {}) == (not ()) == (not False))
? ? print("---output a,b---")
? ? a = []
? ? b = None
? ? print("a=[]")
? ? print("b=None")
? ? print("--- if x")
? ? if a:
? ? ? ? print("a")
? ? else:
? ? ? ? print("None")
? ? if b:
? ? ? ? print("b")
? ? else:
? ? ? ? print("None")
? ? print("--- is None")
? ? if a is None:
? ? ? ? print("None")
? ? else:
? ? ? ? print("a")
? ? if b is None:
? ? ? ? print("None")
? ? else:
? ? ? ? print("b")
? ? print("--- not")
? ? if not a:
? ? ? ? print("None")
? ? else:
? ? ? ? print("a")
? ? if not b:
? ? ? ? print("None")
? ? else:
? ? ? ? print("b")
? ? print("--- is not None")
? ? if a is not None:
? ? ? ? print("a")
? ? else:
? ? ? ? print("None")
? ? if b is not None:
? ? ? ? print("B")
? ? else:
? ? ? ? print("None")以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python中ThreadPoolExecutor線程池和ProcessPoolExecutor進(jìn)程池
這篇文章主要介紹了python中ThreadPoolExecutor線程池和ProcessPoolExecutor進(jìn)程池,文章圍繞主題相關(guān)資料展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-06-06
使用Python中OpenCV和深度學(xué)習(xí)進(jìn)行全面嵌套邊緣檢測(cè)
這篇文章主要介紹了使用Python中OpenCV和深度學(xué)習(xí)進(jìn)行全面嵌套邊緣檢測(cè),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
python解決報(bào)錯(cuò)ImportError: Bad git executable.問(wèn)題
這篇文章主要介紹了python解決報(bào)錯(cuò)ImportError: Bad git executable.問(wèn)題。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
分享5個(gè)短小精悍的Python趣味腳本,適合小白上手!
這篇文章主要給大家分享介紹了5個(gè)短小精悍的Python趣味腳本,非常適合小白上手,分別包含圖片尺寸縮小、pdf轉(zhuǎn)txt文檔、猜數(shù)字游戲、電池電量告警以及圖片添加水印等腳本,需要的朋友可以參考下2022-02-02
全網(wǎng)最新用python實(shí)現(xiàn)各種文件類型轉(zhuǎn)換的方法
這篇文章主要介紹了用python實(shí)現(xiàn)各種文件類型轉(zhuǎn)換的方法,包括word轉(zhuǎn)pdf,excel轉(zhuǎn)pdf,ppt轉(zhuǎn)pdf,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
Python DataFrame.groupby()聚合函數(shù),分組級(jí)運(yùn)算
python的pandas包提供的數(shù)據(jù)聚合與分組運(yùn)算功能很強(qiáng)大,也很靈活,本文就帶領(lǐng)大家一起來(lái)了解groupby技術(shù),感興趣的朋友跟隨小編一起來(lái)看下2018-09-09
Python操作JSON實(shí)現(xiàn)網(wǎng)絡(luò)數(shù)據(jù)交換
這篇文章主要介紹了Python操作JSON實(shí)現(xiàn)網(wǎng)絡(luò)數(shù)據(jù)交換,JSON的全稱是 JavaScript Object Notation,是一種輕量級(jí)的數(shù)據(jù)交換格式,關(guān)于JSON的更多相關(guān)內(nèi)容感興趣的小伙伴可以參考一下2022-06-06
對(duì)Pytorch 中的contiguous理解說(shuō)明
這篇文章主要介紹了對(duì)Pytorch 中的contiguous理解說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03

