Python Membership Operators
Exploring Membership Operators in Python
Membership operators in Python are fundamental tools for checking whether a value is a member of a sequence, such as a string, list, or tuple. These operators, in and not in, provide a concise and expressive way to validate the presence or absence of an element within a collection. In this comprehensive guide, weโll delve into the world of membership operators, their syntax, and their applications in Python programming.
The following table lists the membership operators in Python:
Operator | Description | Example |
---|---|---|
in in | Returns True True if a sequence with the specified value is present in the object | x in y x in y |
not in not in | Returns True True if a sequence with the specified value is not present in the object | x not in y x not in y |
in Operator
in
in
Operator
The in
in
operator returns True
True
if a sequence with the specified value is present in the object. The following example demonstrates how to use the in
in
operator in Python:
# in operator
x = 10
y = 5
z = x in y
t = x in 10
print(z)
print(t)
# in operator
x = 10
y = 5
z = x in y
t = x in 10
print(z)
print(t)
Output:
C:\Users\Your Name> python operators.py
False
True
C:\Users\Your Name> python operators.py
False
True
In the above example, we have used the in
in
operator to check if the value of x
x
is present in the object y
y
. Since the value of x
x
is not present in the object y
y
, the condition becomes False
False
. The result of the in
in
operator is then assigned to the variable z
z
. The value of z
z
is then printed to the console.
not in Operator
not in
not in
Operator
The not in
not in
operator returns True
True
if a sequence with the specified value is not present in the object. The following example demonstrates how to use the not in
not in
operator in Python:
# not in operator
x = 10
y = 5
z = x not in y
t = x not in 10
print(z)
print(t)
# not in operator
x = 10
y = 5
z = x not in y
t = x not in 10
print(z)
print(t)
Output:
C:\Users\Your Name> python operators.py
True
False
C:\Users\Your Name> python operators.py
True
False
In the above example, we have used the not in
not in
operator to check if the value of x
x
is not present in the object y
y
. Since the value of x
x
is not present in the object y
y
, the condition becomes True
True
. The result of the not in
not in
operator is then assigned to the variable z
z
. The value of z
z
is then printed to the console.
Membership Operators with Lists
The membership operators can be used with lists. The following example demonstrates how to use the membership operators with lists in Python:
# Membership operators with lists
x = [1, 2, 3, 4, 5]
y = 10
z = y in x
t = y not in x
print(z)
print(t)
# Membership operators with lists
x = [1, 2, 3, 4, 5]
y = 10
z = y in x
t = y not in x
print(z)
print(t)
Output:
C:\Users\Your Name> python operators.py
False
True
C:\Users\Your Name> python operators.py
False
True
In the above example, we have used the membership operators with lists. The in
in
operator returns True
True
if the value of y
y
is present in the list x
x
. Since the value of y
y
is not present in the list x
x
, the condition becomes False
False
. The not in
not in
operator returns True
True
if the value of y
y
is not present in the list x
x
. Since the value of y
y
is not present in the list x
x
, the condition becomes True
True
.
Conclusion
Membership operators in Python are powerful tools for validating the presence or absence of values within sequences. Whether youโre working with lists, tuples, strings, or sets, in and not in provide a concise and readable syntax for membership testing.
As you advance in your Python programming journey, experiment with membership operators, incorporate them into your conditional statements, and explore their applications in real-world scenarios. For more insights and practical examples, check out our tutorials on Python Central Hub!
Was this page helpful?
Let us know how we did