最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python空值判斷方式(if xxx和if xxx is None的區(qū)別及說明)

 更新時間:2022年11月30日 10:23:27   作者:Urmsone  
這篇文章主要介紹了python空值判斷方式(if xxx和if xxx is None的區(qū)別及說明),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

if xxx 和if xxx is None的區(qū)別

一、 if xxx

None,’’,0,[],{},() ,False都被判斷為空值(not xxx等價)

如下代碼輸出所示,

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

結論:

將空列表換成上述的其他空類型,結果一樣。

如果需要過濾None值和空對象時(如[],{},''等),可使用這種寫法

二、 if xxx is None

該寫法可將None和其他空值對象區(qū)分開來

如下代碼輸出所示:

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

結論:

需要區(qū)分[],{},'',()等空值對象與None的區(qū)別時時可使用這種寫法

貼下簡單的測試代碼

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")

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • 深入理解Python虛擬機中的反序列化pyc文件

    深入理解Python虛擬機中的反序列化pyc文件

    再這篇文章中我們將主要對?Code?Object?進行分析,并且詳細它是如何被反序列化的,通過本篇文章我們將能夠把握整個?pyc?文件結構,感興趣的可以了解一下
    2023-05-05
  • python中ThreadPoolExecutor線程池和ProcessPoolExecutor進程池

    python中ThreadPoolExecutor線程池和ProcessPoolExecutor進程池

    這篇文章主要介紹了python中ThreadPoolExecutor線程池和ProcessPoolExecutor進程池,文章圍繞主題相關資料展開詳細的內容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下
    2022-06-06
  • 使用Python中OpenCV和深度學習進行全面嵌套邊緣檢測

    使用Python中OpenCV和深度學習進行全面嵌套邊緣檢測

    這篇文章主要介紹了使用Python中OpenCV和深度學習進行全面嵌套邊緣檢測,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-05-05
  • python解決報錯ImportError: Bad git executable.問題

    python解決報錯ImportError: Bad git executable.問題

    這篇文章主要介紹了python解決報錯ImportError: Bad git executable.問題。具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 分享5個短小精悍的Python趣味腳本,適合小白上手!

    分享5個短小精悍的Python趣味腳本,適合小白上手!

    這篇文章主要給大家分享介紹了5個短小精悍的Python趣味腳本,非常適合小白上手,分別包含圖片尺寸縮小、pdf轉txt文檔、猜數字游戲、電池電量告警以及圖片添加水印等腳本,需要的朋友可以參考下
    2022-02-02
  • 全網最新用python實現各種文件類型轉換的方法

    全網最新用python實現各種文件類型轉換的方法

    這篇文章主要介紹了用python實現各種文件類型轉換的方法,包括word轉pdf,excel轉pdf,ppt轉pdf,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2023-05-05
  • Python DataFrame.groupby()聚合函數,分組級運算

    Python DataFrame.groupby()聚合函數,分組級運算

    python的pandas包提供的數據聚合與分組運算功能很強大,也很靈活,本文就帶領大家一起來了解groupby技術,感興趣的朋友跟隨小編一起來看下
    2018-09-09
  • Django框架模板介紹

    Django框架模板介紹

    今天小編就為大家分享一篇關于Django框架模板介紹,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Python操作JSON實現網絡數據交換

    Python操作JSON實現網絡數據交換

    這篇文章主要介紹了Python操作JSON實現網絡數據交換,JSON的全稱是 JavaScript Object Notation,是一種輕量級的數據交換格式,關于JSON的更多相關內容感興趣的小伙伴可以參考一下
    2022-06-06
  • 對Pytorch 中的contiguous理解說明

    對Pytorch 中的contiguous理解說明

    這篇文章主要介紹了對Pytorch 中的contiguous理解說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03

最新評論

宁远县| 耿马| 新野县| 白玉县| 怀远县| 宜良县| 泾川县| 乌兰县| 昆山市| 郎溪县| 梁平县| 元朗区| 天气| 花垣县| 陇川县| 微博| 乐清市| 樟树市| 漠河县| 泗洪县| 汪清县| 丰顺县| 亚东县| 云龙县| 南康市| 榆林市| 顺昌县| 绥棱县| 小金县| 阿尔山市| 巴东县| 瑞安市| 屯门区| 汉源县| 萝北县| 洞头县| 浦东新区| 治多县| 凤庆县| 大同县| 调兵山市|