首页 > 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进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐