3. 成员运算符和身份运算符
一:成员运算符
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
-
判断的是id是否相等
-
如果引用的是同一个对象则返回 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
三:短路运算
偷懒原则,偷懒到哪个位置,就把当前位置的值返回
10>3 and 10 and None and 10 //返回None
10>3 and 10 and None and 10 or 10 > 3 and 1 == 1 //返回True