Home Course Roadmap
Home / Python Foundations / Membership & Identity Operators
Lesson 13 · Operators

Python Membership & Identity Operators

Learn how to check whether a value exists inside a collection and how to determine whether two variables refer to the same object in memory.

30–40 minutes
Beginner
Python Foundations
Reading progress 0%

What You'll Learn

  • Understand membership operators in Python.
  • Use in and not in.
  • Check membership in strings and collections.
  • Understand how dictionary membership works.
  • Understand the difference between is and ==.
  • Use identity checks correctly with None.
01

What Are Membership & Identity Operators?

Python provides special operators that allow us to answer two important questions:

  1. Membership: Does this value exist inside another object?
  2. Identity: Are these two variables referring to the exact same object?

Python has four operators for these purposes:

Membership & Identity Operators
# Membership
in
not in

# Identity
is
is not

Simple Way to Remember

in asks: "Is this value inside?"

is asks: "Is this the exact same object?"

02

The in Operator

The in operator checks whether a value exists inside another object.

It returns either True or False.

Example
numbers = [10, 20, 30, 40]

print(20 in numbers)
print(50 in numbers)
Output
True
False

Python searches the collection and checks whether the specified value is present.

03

The not in Operator

The not in operator checks whether a value does not exist inside an object.

Example
numbers = [10, 20, 30]

print(40 not in numbers)
print(20 not in numbers)
Output
True
False
Remember:

not in is simply the opposite of in.

04

Membership with Strings

Membership operators can also be used with strings. Python checks whether one string occurs inside another string.

String Membership
text = "Python Programming"

print("Python" in text)
print("Programming" in text)
print("Java" in text)
print("Java" not in text)
Output
True
True
False
True
Case Sensitivity Matters

String membership is case-sensitive.

text = "Python"

print("Python" in text)
print("python" in text)

The first result is True, while the second is False.

05

Membership with Lists

Lists are one of the most common places where membership operators are used.

List Membership
students = ["Ali", "Sara", "Ahmed", "Fatima"]

print("Sara" in students)
print("Hamza" in students)
print("Ahmed" not in students)
Output
True
False
False

This becomes particularly useful when validating user selections or checking whether an item already exists.

06

Membership with Tuples

Membership operators work with tuples in the same way they work with lists.

Tuple Example
days = ("Monday", "Tuesday", "Wednesday")

print("Monday" in days)
print("Friday" in days)
print("Sunday" not in days)
Output
True
False
True
07

Membership with Sets

Sets are especially useful when you frequently need membership checks.

Set Example
numbers = {10, 20, 30, 40, 50}

print(30 in numbers)
print(100 in numbers)
print(100 not in numbers)
Output
True
False
True

Because sets are designed around unique elements and membership operations, they are often useful for fast existence checks.

08

Membership with Dictionaries

Dictionaries require special attention. When you use in with a dictionary, Python checks the keys by default.

Dictionary Membership
student = {
    "name": "Ali",
    "age": 25,
    "city": "Multan"
}

print("name" in student)
print("age" in student)
print("Ali" in student)
Output
True
True
False

Important Rule

Dictionary membership checks keys by default, not values.

"name" in student     # True
"Ali" in student      # False

If you want to check dictionary values, you can use .values().

Checking Values
student = {
    "name": "Ali",
    "age": 25
}

print("Ali" in student.values())
print(25 in student.values())
Output
True
True
09

Practical Membership Examples

Membership operators become powerful when combined with conditions.

Checking an Allowed Role

Access Check
allowed_roles = ["admin", "manager", "editor"]

role = "manager"

if role in allowed_roles:
    print("Access granted")
else:
    print("Access denied")
Output
Access granted

Checking an Email Domain

Email Example
email = "student@example.com"

if "@example.com" in email:
    print("Valid example domain")
else:
    print("Different domain")

Checking a Product Category

Business Example
categories = ["shoes", "shirts", "bags"]

product_category = "shoes"

if product_category in categories:
    print("Category available")
10

The is Operator

The is operator checks whether two variables refer to the same object.

This is different from checking whether two values are equal.

Basic Identity Example
a = [1, 2, 3]
b = a

print(a is b)
Output
True

Both a and b refer to the same list object.

11

The is not Operator

The is not operator checks whether two variables refer to different objects.

Example
a = [1, 2, 3]
b = [1, 2, 3]

print(a is not b)
Output
True

Although the lists contain the same values, they are two separate list objects.

12

is vs ==

This is one of the most important distinctions in Python.

Comparison vs Identity
a = [1, 2, 3]
b = [1, 2, 3]

print(a == b)
print(a is b)
Output
True
False

The Key Difference

== checks whether two values are equal.

is checks whether two variables refer to the same object.

Same Object
a = [1, 2, 3]
b = a

print(a == b)
print(a is b)
Output
True
True
13

Identity and Object References

To understand is, it helps to understand that variables in Python refer to objects.

Reference Example
numbers = [10, 20, 30]

another = numbers

print(numbers is another)
Output
True

The statement:

another = numbers

does not create a new list. Both variable names refer to the same list object.

Don't confuse equality with identity.

Two separate objects can contain exactly the same values while still being different objects.

You've reached the end of this lesson.

Review the examples, open the Practice Lab, and mark this lesson complete when you're ready.

Open Practice Lab