Python正课12 —— 成员运算 与 身份运算

本文内容皆为作者原创,如需转载,请注明出处:https://www.cnblogs.com/xuexianqi/p/12425826.html

一:成员运算符

1.in

判断一个字符或者字符串是否存在于一个大字符串中

print("eogn" in "hello eogn")
print("e" in "hello eogn")

True
True

判断元素是否存在于列表

print(1 in [1,2,3])
print('x' in ['x','y','z'])

True
True

判断key是否存在于列表

print('k1' in {'k1':111,'k2':222})
print(111 in {'k1':111,'k2':222})

True
False

2.not in

判断一个字符或者字符串是否存在于一个大字符串中

print("eogn" not in "hello eogn")
print("e" not in "hello eogn")

False
False

判断元素是否存在于列表

print(1 not in [1,2,3])
print('x' not in ['x','y','z'])

False
False

判断key是否存在于列表

print('k1' not in {'k1':111,'k2':222})
print(111 not in {'k1':111,'k2':222})

False
True

二:身份运算符

1.is

是判断两个标识符是不是引用自一个对象

如果引用的是同一个对象则返回 True,否则返回 False

x = 1
y = 1
print(x is y)

True
x = 1
y = 2
print(x is y)

False

2.not is

是判断两个标识符是不是引用自不同对象

如果引用的不是同一个对象则返回结果 True,否则返回 False

x = 1
y = 1
print(x is not y)

False
x = 1
y = 2
print(x is not y)

True
原文地址:https://www.cnblogs.com/xuexianqi/p/12425826.html