Skip to content

成员运算符

成员运算符

Python 提供了成员运算符,用于判断实例中是否包含了一系列的成员,包括字符串,列表或元组。

如下表所示:

运算符 描述
in 如果在指定的序列中找到值返回 True,否则返回 False
not in 如果在指定的序列中没有找到值返回 True,否则返回 False

成员运算符 in

如果被查询成员在目标中存在,结果为真 True,如果不存在,结果为假 False

print("o" in "Hogwarts")
print("K" in "Hogwarts")
print(0 in [1,2,3,4,5])
print(3 in [1,2,3,4,5])

成员运算符 not in

如果被查询成员在目标中不存在,结果为真 True,如果存在,结果为假 False

print("o" not in "Hogwarts")
print("K" not in "Hogwarts")
print(0 not in [1,2,3,4,5])
print(3 not in [1,2,3,4,5])