In programming, membership refers to a function that returns whether or not an object is a member of a set. In this blog, you will learn the types of membership operators in python with examples.
Table of Contents
In programming, membership refers to a function that returns whether or not an object is a member of a set. In this blog, you will learn the types of membership operators in python with examples.
Types of Membership Operators
There are two types of membership operators in python language.
- in
- not in
i) in
The in operator checks whether an object is contained within another specified object (usually a list). If the list contains the object, it will return True.
Example 1:
object = [34, 23, 55]
print(34 in object)
Output:
True
Example 2:
language = ["python", "java", "react"]
print("python" in language)
Output:
True
ii) not in
The not in operator returns True if the specified value is not present in the object.
Example 1:
age = [45, 23, 89, 20]
print(33 not in age)
Output:
True
Example 2:
brand = ["hp", "dell", "apple"]
print("dell" not in brand)
Output:
False
Read the full details of Python Operators in this article: