国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

python中的None與NULL用法說明

瀏覽:2日期:2022-06-18 14:32:45

None是一個對象,而NULL是一個類型。

Python中沒有NULL,只有None,None有自己的特殊類型NoneType。

None不等于0、任何空字符串、False等。

在Python中,None、False、0、''(空字符串)、[](空列表)、()(空元組)、{}(空字典)都相當于False。

判斷變量是否為空的高效方法是:

if X is None

if not X:當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 = Noneprint ’X is None測試結果’ print x is None #False print y is None #Trueprint ’not X測試結果’ print not x #True print not y #Trueprint ’not X is None測試結果’ print not x is None #True print not y is None #Falseprint ’X is not None測試結果’ print x is not None #True print y is not None #False

補充:python中None與0、Null、false區別

None是Python中的一個關鍵字,None本身也是個一個數據類型,而這個數據類型就是None,它可0、空字符串以及false均不一樣,這些都只是對象,而None也是一個類。

給個bool測試:

val = Noneif val: print 'None is true'else: print 'None is not true'#輸出None is not true

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 淮安市| 望都县| 深泽县| 厦门市| 海南省| 清流县| 广东省| 高雄县| 昌黎县| 体育| 巴林右旗| 邓州市| 辉县市| 盱眙县| 和政县| 榆林市| 高青县| 鸡东县| 丽水市| 沅江市| 固安县| 京山县| 五寨县| 曲松县| 连南| 灌南县| 舞钢市| 衡阳市| 佛教| 宁南县| 建始县| 郯城县| 杨浦区| 平阳县| 杂多县| 永福县| 松桃| 天长市| 宁津县| 香格里拉县| 西青区|