Python has a smart way of searching that is faster and cleaner. A membership operator checks if something exists within a collection. Today, we will be talking about these in depth. So, let’s understand one of the most unique operators of Python that no other programming language doesn’t support.
Table of Contents
What are Membership Operators?
Membership operators are quite simple and consist of two types: in
and not in
. These operators help on determine whether a particular element exists within a collection, such as a list or a string.
in
Operator
This operator checks if a specific element is present in a collection. If the element is found, it returns True
; otherwise, it returns False
.
For example, suppose you have a list of cricketers:
cricketers = ["Rohit", "Kohli", "Rahul"]
PythonNow, if we want to check if "Katrina"
is in the list of cricketers, we can do the following:
print("Katrina" in cricketers)
# Output: False
PythonSince "Katrina"
is not in the list, the output will be False
.
Now, let’s check for “Rahul”:
print("Rahul" in cricketers)
# Output: True
PythonHere, since "Rahul"
is indeed in the list, the output will be True
.
not in
Operator
This operator checks if a specific element is absent from a collection. If the element is not found, it returns True
; otherwise, it returns False
.
Now, let’s look at the not in
operator. Using the same list of cricketers, we can check if "Katrina"
is not in the list:
print("Katrina" not in cricketers)
# Output: True
PythonSince "Katrina"
is not in the list, the output will be True
.
Problems
- Membership operators work differently depending on the different data types you are using.
For example:
- In a string, they check if one part of the text (called a substring) is inside the whole text.
- In a list, they check if an item exists in the collection of items.
It’s important to remember to use the correct data type. Otherwise, your code might not work as expected.
my_string = "Hello, World!"
print("Hello" in my_string) # True
print("H" in my_string) # True
my_list = ["Hello", "World"]
print("Hello" in my_list) # True
print("H" in my_list) # False
Python- The
in
operator can only be used with iterable types (like lists, tuples, strings, dictionaries, and sets). Using it with non-iterable types (like integers or floats) will raise aTypeError
. For example,
# Correct usage with a list
numbers = [1, 2, 3, 4, 5]
print(5 in numbers) # Output: True
# Incorrect usage with a single number
number = 5
print(5 in number) # Raises TypeError
none = None
print(none in None) # Raises TypeError
Python- When you use
in
with a dictionary in Python, it only looks at the keys. It does not look at the values. This can confuse you if you think it’s also checking the values. For example,
my_dict = {"a": 1, "b": 2, "c": 3}
print("a" in my_dict) # Output: True
print(2 in my_dict) # Output: False
PythonConclusion
In summary, Membership operators in Python are essential for checking the presence or absence of elements in collections. The in
operator helps us confirm if an element exists, while the not in
operator helps us verify if it does not exist.