What are Membership Operators in Python With Example?

Python provides two membership operators:

  • in operator: It checks if a value exists in a sequence and returns True if it does, otherwise False.
  • not in operator: It checks if a value does not exist in a sequence and returns True if it doesn’t, otherwise False.

Here’s an example to illustrate the usage of membership operators:

colors = {'red', 'green', 'blue'}

# Check if 'yellow' is present in the set
if 'yellow' in colors:
    print("'yellow' is present in the colors set.")

# Check if 'green' is not present in the set
if 'green' not in colors:
    print("'green' is not present in the colors set.")
Code language: Python (python)

Output:

'green' is not present in the colors set.Code language: Python (python)

In this example, we have a set called colors. Using the in operator, we check if the string 'yellow' exists in the set. Since it is not present, the condition evaluates to False, and the corresponding message is not printed.

Next, we use the not in operator to check if the string 'green' is not present in the set. As it is found in the set, the condition evaluates to False, and the corresponding message is not printed.

Since the second condition is False, only the first message is not printed in this example.

Example for membership operator in Python

Here are two more examples demonstrating the usage of membership operators in Python:

Example 1: Membership operators with strings

quote = "Be yourself; everyone else is already taken."

# Check if 'yourself' is present in the quote
if 'yourself' in quote:
    print("'yourself' is present in the quote.")

# Check if 'world' is not present in the quote
if 'world' not in quote:
    print("'world' is not present in the quote.")Code language: Python (python)

Output:

'yourself' is present in the quote.
'world' is not present in the quote.Code language: Python (python)

In this example, we have a string called quote. Using the in operator, we check if the substring 'yourself' exists in the quote. Since it is present, the first condition is True, and the corresponding message is printed.

Next, we use the not in operator to check if the substring 'world' is not present in the quote. As it is not found, the condition evaluates to True, and the second message is printed.

Both conditions evaluate to True in this example, so both corresponding messages are printed.

Example 2: Membership operators with tuples

coordinates = (10, 20, 30, 40, 50)

# Check if 30 is present in the tuple
if 30 in coordinates:
    print("30 is present in the coordinates.")

# Check if 60 is not present in the tuple
if 60 not in coordinates:
    print("60 is not present in the coordinates.")Code language: Python (python)

Output:

30 is present in the coordinates.
60 is not present in the coordinates.
Code language: Python (python)

In this example, we have a tuple called coordinates. Using the in operator, we check if the value 30 exists in the tuple. Since it is present, the first condition is True, and the corresponding message is printed.

Similarly, we use the not in operator to check if the value 60 is not present in the tuple. As it is not found, the condition evaluates to True, and the second message is printed.

Read More;

  • I am a middle python software engineer with a bachelor's degree in Software Engineering from Kharkiv National Aerospace University. My expertise lies in Python, Django, Flask, Docker, REST API, Odoo development, relational databases, and web development. I am passionate about creating efficient and scalable software solutions that drive innovation in the industry.

    View all posts

Leave a Comment