Membership Operators, as the name suggests, test for membership in a sequence such as strings, lists, or tuples.
Python supports the following membership operators:
- IN Operator
- NOT IN Operator
IN Operator
This operator returns True if a variable is found in the specified sequence and False otherwise.
For example, a in nums returns 1 if a is a member of nums.
Example on IN Operator:
a=["red","green","blue","yellow"] if("red" in a): print("Red exists in the list") else: print("Red does not exist in the list")
Output:
Red exists in the list
NOT IN Operator
This operator returns True if a variable is not found in the specified sequence and False otherwise.
For example, a not in nums returns 1 if a is not a member of nums.
Example on not in Operator:
a=["Apple","Banana","Mango"] if("Litchi" not in a): print("Litchi does not exist in the list") else: print("Litchi exists in the list")
Output:
Litchi does not exist in the list
Leave a comment