python中的None與NULL用法說明
None是一個對象,而NULL是一個類型。
Python中沒有NULL,只有None,None有自己的特殊類型NoneType。
None不等于0、任何空字符串、False等。
在Python中,None、False、0、""(空字符串)、[](空列表)、()(空元組)、{}(空字典)都相當(dāng)于False。
判斷變量是否為空的高效方法是:
if X is None
if not X:當(dāng)X為None、False、""、0、[]、()、{}時,not X為真,無法分辨
if not X is None:等價于if not (X is None)、if X is not None
判斷空使用指南
if X is not None寫法清晰明了,且不會出錯,推薦使用;
if not x使用前,必須確定X為None、False、""、0、[]、()、{}時對判斷無影響。
示例
x = [] y = None print 'X is None測試結(jié)果' print x is None #False print y is None #True print 'not X測試結(jié)果' print not x #True print not y #True print 'not X is None測試結(jié)果' print not x is None #True print not y is None #False print 'X is not None測試結(jié)果' print x is not None #True print y is not None #False
補(bǔ)充:python中None與0、Null、false區(qū)別
None是Python中的一個關(guān)鍵字,None本身也是個一個數(shù)據(jù)類型,而這個數(shù)據(jù)類型就是None,它可0、空字符串以及false均不一樣,這些都只是對象,而None也是一個類。
給個bool測試:
val = None if val: print "None is true" else: print "None is not true" #輸出 None is not true
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
淺談哪個Python庫才最適合做數(shù)據(jù)可視化
數(shù)據(jù)可視化是任何探索性數(shù)據(jù)分析或報告的關(guān)鍵步驟,目前有許多非常好的商業(yè)智能工具,比如Tableau、googledatastudio和PowerBI等,本文就詳細(xì)的進(jìn)行對比,感興趣的可以了解一下2021-06-06Tornado Application的實(shí)現(xiàn)
本文主要介紹了Tornado Application的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05Python練習(xí)之制作企業(yè)獎金計(jì)算器
在本篇博客中,我們將使用Python代碼解決一個企業(yè)獎金計(jì)算的問題,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-06-06python numpy庫np.percentile用法說明
這篇文章主要介紹了python numpy庫np.percentile用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06python實(shí)現(xiàn)一個簡單的web應(yīng)用框架
這篇文章主要為大家介紹了使用python寫一個簡單的web應(yīng)用框架實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04Python 用Redis簡單實(shí)現(xiàn)分布式爬蟲的方法
本篇文章主要介紹了Python 用Redis簡單實(shí)現(xiàn)分布式爬蟲的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11