Python—None

None是一个特殊的常量。
None不是False。
None不是0。
None不是空字符串。
None有自己的数据类型NoneType,并且是NoneType中唯一的值。
None只是一个空值的对象,可以将None赋值给任何变量,但不能创建其他NoneType对象。


Python中哪些形式的数据为空呢?
 
常量None
常量False
空列表
空元组
空集合
空字典
整数0
浮点数0.0
空字符串''


None一般用于函数中表示参数的缺省

def func(a, b=None):
    if b is None:
        print('b is None')
    if a is not None:
        print('a :', a)
        a = None
        print('a :', a)
        print('a is not None :', a is not None)
        print('not None :', not None)
    return None

if not func(666):
    print('not func(666) -> True')

 
输出结果:

b is None
a : 666
a : None
a is not None : False
not None : True
not func(666) -> True

 


最后来加深一下印象

bool(None) # False
not None is bool(not None) # True
# How to use ↓
object is None # None和任何其他数据类型对象比较永远返回False
object is not None

转载于:https://www.cnblogs.com/malinqing/p/11285437.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值