在本系列中,我们将探讨python的一些基础知识。今天,我们将详细介绍成员操作符的第一部分:示例。
成员操作符用于判断某个元素是否存在于列表中,或某个子字符串是否存在于目标字符串中。
接下来,我们将展示如何使用成员操作符的代码示例。
代码语言:Python 代码运行次数:0
运行 复制list_1 = [1, 2, 3, 4, "a", "b"] if "a" in list_1: print("a is found") else: print("a is not found") if "c" not in list_1: print("c is not found") str_1 = "Today is sunny" if "o" in str_1: print("o is found") print("\ndone")
代码截图
运行结果
最后,我们来解读一下部分代码:
in成员操作符会返回一个布尔值。
in表示存在时返回
True,
not in表示不存在时返回
True。对于字符串的效果与列表是一样的。

st_1:
print("a is found")
else:
print("a is not found")
if "c" not in list_1:
print("c is not found")
str_1 = "Today is sunny"
if "o" in str_1:
print("o is found")
print("\ndone")






