首页 > Python资料 博客日记
Python成员运算符示例
2023-07-31 14:00:43Python资料围观169次
Python资料网推荐Python成员运算符示例这篇文章给大家,欢迎收藏Python资料网享受知识的乐趣
Python成员运算符测试给定值是否为序列中的成员,例如字符串,列表或元组。Python中有两个成员运算符,如下所述 -
运算符 | 描述 | 示例 |
---|---|---|
in |
如果在指定的序列中找到一个变量的值,则返回true ,否则返回false 。 |
- |
not in |
如果在指定序列中找不到变量的值,则返回true ,否则返回false 。 |
- |
实例
假设变量a
的值为True
,变量b
的值为False
,参考以下代码实现 -
#!/usr/bin/python3
#coding=utf-8
#save file: membership_operators_example.py
a = 10
b = 20
list = [1, 2, 3, 4, 5 ]
if ( a in list ):
print ("Line 1 - a is available in the given list")
else:
print ("Line 1 - a is not available in the given list")
if ( b not in list ):
print ("Line 2 - b is not available in the given list")
else:
print ("Line 2 - b is available in the given list")
c=b/a
if ( c in list ):
print ("Line 3 - a is available in the given list")
else:
print ("Line 3 - a is not available in the given list")
将上面代码保存到文件: membership_operators_example.py 中,执行结果如下 -
F:\worksp\python>python membership_operators_example.py
Line 1 - a is not available in the given list
Line 2 - b is not available in the given list
Line 3 - a is available in the given list
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签:
上一篇:Python安装和环境配置
下一篇:Python数据噪音
相关文章
最新发布
- boto3:Python连接S3对象存储并进行文件操作(上传、下载、删除)
- 全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
- Python pycryptodome类库使用学习总结
- import torch 报错:WinError 126
- 如何在Java中实现邮件发送功能?
- python之openpyxl模块——实现Excel表格的处理(万字教学,全网最全,超详细!)
- BUG解决:postman可以请求成功,但Python requests请求报403
- 解决Pycharm下载torch报错:OSError: [WinError 126] 找不到指定的模块。
- python创建线程和结束线程
- 精通Python(基础篇)pip命令大全(非常详细)零基础入门到精通,收藏这一篇就够了
点击排行
- 版本匹配指南:Numpy版本和Python版本的对应关系
- Python 可视化 web 神器:streamlit、Gradio、dash、nicegui;低代码 Python Web 框架:PyWebIO
- 版本匹配指南:PyTorch版本、torchvision 版本和Python版本的对应关系
- 相关性分析——Pearson相关系数+热力图(附data和Python完整代码)
- Could not build wheels for llama-cpp-python, which is required to install pyproject.toml-based proj
- Python pyinstaller打包exe最完整教程
- Anaconda版本和Python版本对应关系(持续更新...)
- Python与PyTorch的版本对应