if not用法

python中判断变量是否为None三种写法:

  1、if x is None

  2、if not x

  3、if not x is None  理解成 if not (x is None) 结果是和1相反的

 

python中None、false、""、0、[]、{}、()时,采用not 方法判断是相等的

not None==not false==not ''==not 0==not[]==not{}==not()

>>> x = []
>>> y = None
>>> 
>>> x is None
False
>>> y is None
True
>>> 
>>> 
>>> not x
True
>>> not y
True
>>> 
>>> 
>>> not x is None
>>> True
>>> not y is None
False

 

posted @ 2021-03-03 15:41  tooltime  阅读(1391)  评论(0编辑  收藏  举报