Membership Operators
Python membership operators are used to check for membership of objects in sequence, such as string, list, tuple. It checks whether the given value or variable is present in a given sequence. If present, it will return True else False. In Python, there are two membership operators.
in: It returns a result asTrueif it finds a given object in the sequence. Otherwise, it returnsFalse.
Example:
My_list = [11, 15, 21, 29, 50, 70]
number = 15
if number in My_list:
print("number is present")
else:
print("number is not present")
Output:
number is present
not in: It returnsTrueif the object is not present in a given sequence. Otherwise, it returnsFalse.
Example:
my_tuple = (11, 15, 21, 29, 50, 70)
number = 35
if number not in my_tuple:
print("number is not present")
else:
print("number is present")
Output:
number is not present