python 的 if not

今天突然发现python还有一个if not 的判断句,遂先记下
if not系列语句主要有三种用法:

  1. if x is None
  2. if not x
  3. if not x is None

代码举例:

>>> x = 0
>>> not x
True
>>> x is None
False
>>> x
0
>>> not x is None
True
>>> x = None
>>> x is None
True
>>> not x is None
False
>>> x is not None
False

在 python 中,None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()都相当于False
所以使用if not 和if x is None 要注意除None以外其他false状况的影响
而if not x is None 语义不明,更加推荐使用x is not None,这也是Google推荐的写法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值