Python里面None True False之间的区别

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

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

在这里插入图片描述
区分None和False.使用is来操作!

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

在这里插入图片描述
虽然是个小小的区别!但是在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")
...

在这里插入图片描述

进行数据测验:

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

在这里插入图片描述
以上说明,None,False,True还是有很大不同的~

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值