久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

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

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測試結(jié)果’ print x is None #False print y is None #Trueprint ’not X測試結(jié)果’ print not x #True print not y #Trueprint ’not X is None測試結(jié)果’ print not x is None #True print not y is None #Falseprint ’X is not None測試結(jié)果’ print x is not None #True print y is not None #False

補充:python中None與0、Null、false區(qū)別

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

給個bool測試:

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

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 通江县| 兴业县| 彭泽县| 余干县| 铁岭县| 涡阳县| 兴和县| 噶尔县| 沐川县| 金湖县| 昂仁县| 莱阳市| 高碑店市| 来宾市| 准格尔旗| 桂林市| 汝阳县| 牟定县| 张家川| 饶平县| 射洪县| 大港区| 佳木斯市| 图们市| 汉源县| 防城港市| 安泽县| 富阳市| 龙游县| 临猗县| 建平县| 玛多县| 内丘县| 隆子县| 松潘县| 枣强县| 台江县| 大丰市| 尼木县| 泰和县| 龙井市|