0
点赞
收藏
分享

微信扫一扫

Python里面None True False之间的区别


None虽然跟True False一样都是布尔值。
虽然None不表示任何数据,但却具有很重要的作用。
它和False之间的区别还是很大的!
例子:

>>> t = None
>>> if t:
... print("something")
... else:
... print("nothing")
...
nothing

Python里面None True False之间的区别_数据


区分None和False.使用is来操作!

>>> if t is None:
... print("this is None!")
... else:
... print("this is ELSE!")
...
this is None!
>>>

Python里面None True False之间的区别_数据_02


虽然是个小小的区别!但是在Python里面是重要的。你需要将None和不含任何值的空数据结构区分开。

0值的整型/浮点型,空字符串(‘ ’),空列表([]),空元组({}),空集合(set())都是等价于False,但是不等于None。

现在,写一个函数:

>>> def oj(t):
... if t is None:
... print("this is None")
... elif t:
... print("this is True")
... else:
... print("this is False")
...

Python里面None True False之间的区别_数据结构_03

进行数据测验:

>>> oj(None)
this is None
>>> oj(True)
this is True
>>> oj(False)
this is False
>>> oj(0)
this is False
>>> oj(0.0)
this is False
>>> oj([])
this is False
>>> oj(())
this is False
>>> oj({})
this is False

Python里面None True False之间的区别_数据_04


以上说明,None,False,True还是有很大不同的~


举报

相关推荐

0 条评论