【说站】python判断变量的方法对比
2023-06-13 09:13:23 时间
python判断变量的方法对比
1、if x is not None是最好的写法,清晰,不会出现错误。
2、使用if not x这种写法的前提是:必须清楚x等于None、 False、 空字符串""、 空列表[]、空字典{}、空元组()时对你的判断没有影响才行。
三种方法
第一种是if x is None
第二种是 if not x:
第三种是if not x is None
#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
>>>
以上就是python判断变量的方法对比,希望对大家有所帮助。
相关文章
- python求逆矩阵的方法,Python 如何求矩阵的逆「建议收藏」
- python语言变量命名规则有什么_Python变量命名规则(超级详细)
- python编程前景_Python前景如何,学完后可以从事方向?
- python安装不了whl文件_Python安装whl文件过程图解
- python-列表(list)使用方法
- unboundlocalerror python_Python问题:UnboundLocalError: local variable ‘xxx’ referenced before assignme
- json转字符串 python_Python读取json
- 【说站】python传递实参的方法
- 【说站】python变量声明为全局变量的两种方法
- 实战 | 如何用 Python 统计 Jira 数据并可视化
- 【Python面向对象】类方法和静态方法
- 遗传算法的应用实例python实现_遗传算法Python解决一个问题
- PYTHON用户流失数据挖掘:建立逻辑回归、XGBOOST、随机森林、决策树、支持向量机、朴素贝叶斯和KMEANS聚类用户画像|附代码数据
- python win32api sendmessage_Python win32api.SendMessage方法代码示例[通俗易懂]
- python转换数据类型
- Python字符串对齐方法(ljust()、rjust()和center())详解
- Python datetime模块常用方法
- 快速查询MySQL数据库:Python篇(python查询mysql数据库)
- python实现在目录中查找指定文件的方法
- 用python删除java文件头上版权信息的方法
- Python采用raw_input读取输入值的方法