Skip to content

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:

OperatorDescriptionExample
ininReturns TrueTrue if a sequence with the specified value is present in the objectx in yx in y
not innot inReturns TrueTrue if a sequence with the specified value is not present in the objectx not in yx not in y

in Operator

inin Operator

The inin operator returns TrueTrue if a sequence with the specified value is present in the object. The following example demonstrates how to use the inin operator in Python:

operators.py
# in operator
x = 10
y = 5
z = x in y
t = x in 10
print(z)
print(t)
operators.py
# in operator
x = 10
y = 5
z = x in y
t = x in 10
print(z)
print(t)

Output:

command
C:\Users\Your Name> python operators.py
False
True
command
C:\Users\Your Name> python operators.py
False
True

In the above example, we have used the inin operator to check if the value of xx is present in the object yy. Since the value of xx is not present in the object yy, the condition becomes FalseFalse. The result of the inin operator is then assigned to the variable zz. The value of zz is then printed to the console.

not in Operator

not innot in Operator

The not innot in operator returns TrueTrue if a sequence with the specified value is not present in the object. The following example demonstrates how to use the not innot in operator in Python:

operators.py
# not in operator
x = 10
y = 5
z = x not in y
t = x not in 10
print(z)
print(t)
operators.py
# not in operator
x = 10
y = 5
z = x not in y
t = x not in 10
print(z)
print(t)

Output:

command
C:\Users\Your Name> python operators.py
True
False
command
C:\Users\Your Name> python operators.py
True
False

In the above example, we have used the not innot in operator to check if the value of xx is not present in the object yy. Since the value of xx is not present in the object yy, the condition becomes TrueTrue. The result of the not innot in operator is then assigned to the variable zz. The value of zz 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:

operators.py
# 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)
operators.py
# 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:

command
C:\Users\Your Name> python operators.py
False
True
command
C:\Users\Your Name> python operators.py
False
True

In the above example, we have used the membership operators with lists. The inin operator returns TrueTrue if the value of yy is present in the list xx. Since the value of yy is not present in the list xx, the condition becomes FalseFalse. The not innot in operator returns TrueTrue if the value of yy is not present in the list xx. Since the value of yy is not present in the list xx, the condition becomes TrueTrue.

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