Python运算符(6)-成员运算符

Python语言支持以下类型的运算符:
  1、算术运算符
  2、比较(关系)运算符
  3、赋值运算符
  4、逻辑运算符
  5、位运算符
  6、成员运算符
  7、身份运算符
  8、运算符优先级

6、成员运算符

  Python支持成员运算符,测试实例中包含了一系列的成员,包括字符串,列表或元组。

运算符 描述 实例
in 如果在指定的序列中找到值返回 True
否则返回 False
x 在 y 序列中
如果 x 在 y 序列中返回 True
not in 如果在指定的序列中没有找到值返回 True
否则返回 False
x 不在 y 序列中
如果 x 不在 y 序列中返回 True。

实例代码:

test_String = '0123456789'
test_List = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
test_Tuple = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
print('测试字符串为:{}'.format(test_String))
print('测试列表为:{}'.format(test_List))
print('测试元组为:{}'.format(test_Tuple))
x = input('请输入x的值:')
y = input('请输入y的值:')

if x in test_String:
   print('“{}”在“{}”中'.format(x, test_String))
else:
   print('“{}”不在“{}”中'.format(x, test_String))

if y not in test_String:
   print('“{}”不在“{}”中'.format(y, test_String))
else:
   print('“{}”在“{}”中'.format(y, test_String))

if x in test_List:
   print('“{}”在“{}”中'.format(x, test_List))
else:
   print('“{}”不在“{}”中'.format(x, test_List))

if y not in test_List:
   print('“{}”不在“{}”中'.format(y, test_List))
else:
   print('“{}”在“{}”中'.format(y, test_List))

if x in test_Tuple:
   print('“{}”在“{}”中'.format(x, test_Tuple))
else:
   print('“{}”不在“{}”中'.format(x, test_Tuple))

if y not in test_Tuple:
   print('“{}”不在“{}”中'.format(y, test_Tuple))
else:
   print('“{}”在“{}”中'.format(y, test_Tuple))

运行结果:

测试字符串为:0123456789
测试列表为:['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
测试元组为:('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
请输入x的值:8
请输入y的值:88
“8”在“0123456789”中
“88”不在“0123456789”中
“8”在“['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']”中
“88”不在“['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']”中
“8”在“('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')”中
“88”不在“('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')”中
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 这是16年5月份编辑的一份比较杂乱适合自己观看的学习记录文档,今天18年5月份再次想写文章,发现简书还为我保存起的...
    Jenaral阅读 8,076评论 2 9
  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 10,034评论 0 5
  • 1. 《舞!舞!舞!》(日)村上春树,林少华翻译 2.《巴黎的忧郁》(法)波德莱尔 3. 《身份的焦虑》(英)阿兰...
    Annie_Shoo阅读 1,171评论 0 0
  • 相信每个人都有自己的故事 自己兄弟闺蜜 兄弟闺蜜是我们步入社会的资本 没有兄弟 闺蜜 哪来的出人头地 哪来的精神支...
    刘认真吖阅读 1,549评论 0 1
  • 2017或许会有些问题。 2016年12月27日和婆婆一起去芽庄旅行。行程安排得很松散,既然是放假就想完全放松一下...
    慕七七_阅读 1,517评论 0 0

友情链接更多精彩内容