Skip to content

Python Membership Operators

How to use Python's in and not in operators to check if values exist in sequences like lists, tuples, or strings with practical examples.

Membership Operators

Membership operators are used to test whether a value or variable is in a sequence.

Operator Description Example
in True if value/variable is found in the sequence 2 in x
not in True if value/variable is not found in the sequence 2 not in x

Examples

my_list = [1,2,3,4,5,6,7,8,9,10]
a = 5
b = 50

print(a in my_list)     # True
print(a not in my_list) # False

print(b in my_list)     # False
print(b not in my_list) # True

print(1 in my_list)     # True
print(1 not in my_list) # False
print(20 in my_list)    # False
print(20 not in my_list)# True
Navigation