7. Membership Operators

Membership operators are used to check if a value is present in a sequence (like string, list, tuple, set, or dictionary). They return True if the value is found, otherwise False.

These are mostly used in if or while conditions. In a dictionary, they check only for keys, not values.

Membership operators are useful for searching, validating data, and filtering values. In case of strings, the check is case-sensitive. For example, 'A' in 'apple' will return False.

Types of Identity Operators:

There are 2 types of Identity Operators in Python:
  • in
  • not in

List of Membership Operators

Operator Name Description
in Membership Operator Returns True if the specified value is present in the sequence.
not in Negative Membership Returns True if the specified value is not present in the sequence.

Types of Membership Operators

  • in – Returns True if the value is found in the sequence.
  • not in – Returns True if the value is not found in the sequence.

Types of Membership Operators:

  1. Membership Operator (in):

    The in operator checks if a value exists inside a sequence.

    fruits = ["apple", "banana", "mango"]
    print("banana" in fruits)   # True

    Output: True

    Explanation:

    Here, "banana" is present in the list called fruits, so "banana" in fruits returns True.

  2. Negative Membership Operator (not in):

    The not in operator checks if a value does not exist in a sequence.

    colors = ["red", "green", "blue"]  
    print("yellow" not in colors)   # True

    Output: True

    Explanation:

    Here, "yellow" is not found in the colors list, so "yellow" not in colors gives True.

Where it is Used?

  • To check if an item exists in a list, string, tuple, set, etc.
  • Commonly used in if statements, loops, and filtering.
  • Helpful in checking presence or absence of values.

Example:

my_list = [1, 2, 3, 4, 5]

print(3 in my_list)        # True
print(7 in my_list)        # False
print(6 not in my_list)    # True
print(2 not in my_list)    # False

Explanation:

  • 3 in my_listTrue because 3 is present in the list.
  • 7 in my_listFalse because 7 is not present.
  • 6 not in my_listTrue because 6 is missing from the list.
  • 2 not in my_listFalse because 2 exists in the list.

Note: These operators are most useful in conditions, filtering, and validating data from collections like lists, strings, and dictionaries.

Summary

  • in → Checks if value is present in sequence
  • not in → Checks if value is not present in sequence
  • Returns boolean values: True or False
  • Useful in decision-making and filtering

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar