python判断变量的方法对比

543次阅读
没有评论

python判断变量的方法对比

python变量判断的三种方法

第一种是if x is None

第二种是 if not x:

第三种是if not x is None

python变量判断实例

if x is not None是最好的写法,清晰,不会出现错误。

使用if not x这种写法的前提是:必须清楚x等于None, False, 空字符串””, 0, 空列表[], 空字典{}, 空元组()时对你的判断没有影响才行。

#Python
 
>>> 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
>>>
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-04-24发表,共计326字。
新手QQ群:570568346,欢迎进群讨论 Python51学习