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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM