Python运算符

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

2020-03-14  本文已影响0人  复苏的兵马俑

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')”中
上一篇 下一篇

猜你喜欢

热点阅读