Python成员运算符

2025年6月2日 | 阅读 9 分钟

在 Python 中,**成员运算符**是一些额外的运算符,可以帮助我们检查一个**指定值或元素是否存在**于特定的序列或集合中,例如 字符串列表元组集合字典。换句话说,这些运算符用于测试一个项是否存在于给定的序列中。

成员运算符的类型

Python 提供了**两种类型的成员运算符**,允许我们检查或验证值的成员资格。

运算符描述
in如果值存在于序列中,则返回 True
不在如果值不存在于序列中,则返回 True

让我们通过不同的示例来讨论这些运算符。

Python 'in' 运算符

'in' 运算符是成员运算符之一,用于检查一个项是否存在于给定的集合中(例如,字符串、列表、元组等)。

现在让我们看下面的例子

示例

立即执行

输出

Is 'mango' a member of the 'fruits' list? True
Is 'banana' a member of the 'fruits' list? False

说明

在此示例中,我们使用 'in' 运算符检查 'mango' 和 'banana' 是否为给定列表的成员。结果是,此运算符对 'mango' 返回 True,对 'banana' 返回 False。

让我们看另一个示例,我们将检查一个子字符串是否是给定字符串的一部分。

示例

立即执行

输出

Is 'po' a substring of 'Tpoint Tech'? True
Is 'ech' a substring of 'Tpoint Tech'? True
Is 'Tp' a substring of 'Tpoint Tech'? True
Is 'Nte' a substring of 'Tpoint Tech'? False

说明

在这里,我们使用 'in' 运算符来检查初始化的子字符串是否是给定字符串的一部分。

Python 'not in' 运算符

'not in' 运算符是另一个成员运算符,用于检查一个项是否不存在于给定的序列或集合中(例如,字符串、列表、元组等)。

让我们看下面的示例

示例

立即执行

输出

Is 'onion' NOT a member of the 'vegetables' list? True
Is 'carrot' NOT a member of the 'vegetables' list? False

说明

在此示例中,我们使用 'not in' 运算符来检查 'onion' 和 'carrot' 是否不是给定列表的成员。结果是,此运算符对 'onion' 返回 True,对 'carrot' 返回 False。

让我们看另一个示例,检查给定的子字符串是否不存在于字符串中。

示例

立即执行

输出

Is 'ak' NOT a substring of 'Tpoint Tech'? True
Is 'ech' NOT a substring of 'Tpoint Tech'? False
Is 'br' NOT a substring of 'Tpoint Tech'? True
Is 'nt' NOT a substring of 'Tpoint Tech'? False

说明

在这里,我们使用 'not in' 运算符来检查初始化的子字符串是否是给定字符串的一部分。

数据结构中的成员运算符

我们可以使用 'in' 和 'not in' 运算符来检查项或元素在给定的数据结构中的成员资格,例如列表、元组、集合和字典。

让我们看一些例子

示例 1:列表中的成员运算符

在下面的示例中,我们将使用成员运算符(in/not in)与列表一起检查指定的项是否存在于其中。

示例

立即执行

输出

Is '16' a member of [12, 16, 17, 21, 26] : True
Is '14' a member of [12, 16, 17, 21, 26] : False
Is '11' NOT a member of [12, 16, 17, 21, 26] : True
Is '12' NOT a member of [12, 16, 17, 21, 26] : False

Is '[3, 7]' a member of [1, [3, 7], 12, 5] : True
Is '7' a member of [1, [3, 7], 12, 5] : False
Is '[2, 6]' NOT a member of [1, [3, 7], 12, 5] : True
Is '5' NOT a member of [1, [3, 7], 12, 5] : False

说明

在这里,我们使用了 'in' 和 'not in' 运算符以及给定的列表和嵌套列表。对于 list_1,它检查了某些项是否存在。对于 list_2,它检查了元素的成员资格,包括一个嵌套列表。请注意,它仅检查顶级元素。因此,对于 7,它返回 False,而对于 [3, 7] 返回 True。

示例 2:元组中的成员运算符

让我们看一个示例,展示了如何在元组中使用成员运算符(in/not in)来检查指定的项是否存在于其中。

示例

立即执行

输出

Is '16' a member of (12, 16, 17, 21, 26) : True
Is '14' a member of (12, 16, 17, 21, 26) : False
Is '11' NOT a member of (12, 16, 17, 21, 26) : True
Is '12' NOT a member of (12, 16, 17, 21, 26) : False

Is '(3, 7)' a member of (1, (3, 7), 12, 5) : True
Is '7' a member of (1, (3, 7), 12, 5) : False
Is '(2, 6)' NOT a member of (1, (3, 7), 12, 5) : True
Is '5' NOT a member of (1, (3, 7), 12, 5) : False

说明

在这里,我们使用了 'in' 和 'not in' 运算符以及给定的元组和嵌套元组。对于 tuple_1,它检查了某些项是否存在。对于 tuple_2,它检查了元素的成员资格,包括一个嵌套元组。与 Python 列表类似,它对于 7 返回 False,而对于 (3, 7) 返回 True。

示例 3:集合中的成员运算符

现在,我们将看一个示例,展示了如何在集合中使用成员运算符(in/not in)来检查指定的项是否存在于其中。

示例

立即执行

输出

Is '16' a member of {16, 17, 21, 26, 12} : True
Is '14' a member of {16, 17, 21, 26, 12} : False
Is '11' NOT a member of {16, 17, 21, 26, 12} : True
Is '12' NOT a member of {16, 17, 21, 26, 12} : False

Is '(3, 7)' a member of {1, 12, 5, (3, 7)} : True
Is '7' a member of {1, 12, 5, (3, 7)} : False
Is '(2, 6)' NOT a member of {1, 12, 5, (3, 7)} : True
Is '5' NOT a member of {1, 12, 5, (3, 7)} : False

说明

在这里,我们使用了 'in' 和 'not in' 运算符以及给定的集合和嵌套集合。对于 set_1,它检查了某些项是否存在。对于 set_2,它检查了元素的成员资格,包括一个嵌套元组。与列表和元组类似,它对于 7 返回 False,而对于 (3, 7) 返回 True,因为它只检查顶级元素。

示例 4:字典中的成员运算符

在 Python 中,我们也可以在字典中使用 'in' 和 'not in' 运算符。但是,Python 只检查键的成员资格,而不检查关联的值。

让我们看下面的示例,以了解如何在字典中使用成员运算符(in/not in)。

示例

立即执行

输出

Is '104' a member of {103: 'john', 104: 'sachin', 105: 'mike', 106: 'mark'} : True
Is 'mark' a member of {103: 'john', 104: 'sachin', 105: 'mike', 106: 'mark'} : False
Is '105' NOT a member of {103: 'john', 104: 'sachin', 105: 'mike', 106: 'mark'} : False
Is 'john' NOT a member of {103: 'john', 104: 'sachin', 105: 'mike', 106: 'mark'} : True

说明

在这里,我们使用了 'in' 和 'not in' 运算符以及给定的字典。对于 'in' 运算符,它检查指定的键是否是给定字典的一部分。结果是,对于键 = 104,它返回 True,对于值 = 'mark',它返回 False。

同样,对于 'not in' 运算符,它检查指定的键是否不是给定字典的一部分。因此,对于 105(键),它返回 False,而对于 'john'(值),它返回 True。

结论

成员运算符 'in' 和 'not in' 是 Python 中的运算符,它们简单而有用,可以帮助我们检查一个值是否存在于列表、字符串、元组、集合甚至字典中。这些运算符返回 True 或 False,可用于验证条件、输入或控制程序逻辑。通过正确理解,这些运算符可以帮助编写更格式化、更高效的 Python 代码。

Python 成员运算符常见问题解答

1. Python 中的成员运算符是什么?

这些是检查值是否存在于列表、字符串、元组、集合或字典等序列中的运算符。它们的返回值将是 **True** 或 **False**。

2. Python 中有两种成员运算符?

  • **in** 在给定值是序列的一部分时返回 **True**。
  • **not in** 在给定值不是序列的一部分时返回 **True**。

3. 成员运算符可以用于字符串吗?

是的,可以使用 **in** 和 **not in** 运算符来检查子字符串是否存在于字符串中。

4. 成员运算符如何与字典一起使用?

对于字典,成员运算符仅检查键是否存在,而不检查值。

5. 成员运算符是否适用于集合或元组

是。**in** 和 **not in** 都可用于集合和元组来检查元素是否存在。