In addition to the operators discussed previously, Python has membership operators, which test for membership in a sequence, such as strings, lists or tuples. There are two membership operators explained below:
| Operator | Description | Example |
| In | Evaluates to true if it finds a variable in the specified sequence and false otherwise. | x in y, here in results in a 1 if x is a member of sequence y. |
| not in | Evaluates to true if it does not finds a variable in the specified sequence and false otherwise. | x not in y, here not in results in a 1 if x is not a member of sequence y. |
Example:
Try following example to understand all the membership operators available in Python programming language:
#!/usr/bin/python
a = 10
b = 20
list = [1, 2, 3, 4, 5 ];
if ( a in list ):
print "Line 1 - a is available in the given list"
else:
print "Line 1 - a is not available in the given list"
本文详细介绍了Python编程中用于测试序列成员性的两个重要运算符:in和not in。通过实例演示了如何使用这些运算符来检查变量是否存在于指定的序列中,包括字符串、列表和元组。
556

被折叠的 条评论
为什么被折叠?



