Membership Operator in Python

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.

Membership Operator in Python

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"]
Python

Now, if we want to check if "Katrina" is in the list of cricketers, we can do the following:

print("Katrina" in cricketers)  
# Output: False
Python

Since "Katrina" is not in the list, the output will be False.

Now, let’s check for “Rahul”:

print("Rahul" in cricketers)  
# Output: True
Python

Here, 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
Python

Since "Katrina" is not in the list, the output will be True.

Problems

  1. 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
  1. 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 a TypeError. 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
  1. 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
Python

Conclusion

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.

Membership Operator in Python

Iterables only

1 / 4

Which of the following data types can be used with membership operators in Python?

Checks dictionary keys

2 / 4

When using the “in” operator with a dictionary, what does it check by default?

Returns True if missing

3 / 4

What does the “not in” operator return when an element is absent from a collection?

Checks existence in collection

4 / 4

What does the membership operator “in” do in Python?

Your score is

The average score is 0%

0%

Leave a Reply

Your email address will not be published. Required fields are marked *