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

 

相关文章:

  • 2018-03-26
  • 2022-12-23
  • 2022-12-23
  • 2021-08-03
  • 2021-06-08
  • 2022-02-18
猜你喜欢
  • 2021-06-29
  • 2021-07-28
  • 2021-08-21
  • 2021-11-16
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案