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.
What You'll Learn
- Understand membership operators in Python.
- Use
inandnot in. - Check membership in strings and collections.
- Understand how dictionary membership works.
- Understand the difference between
isand==. - Use identity checks correctly with
None.
What Are Membership & Identity Operators?
Python provides special operators that allow us to answer two important questions:
- Membership: Does this value exist inside another object?
- Identity: Are these two variables referring to the exact same object?
Python has four operators for these purposes:
# 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?"
The in Operator
The in operator checks whether a value exists
inside another object.
It returns either True or False.
numbers = [10, 20, 30, 40]
print(20 in numbers)
print(50 in numbers)
True
False
Python searches the collection and checks whether the specified value is present.
The not in Operator
The not in operator checks whether a value
does not exist inside an object.
numbers = [10, 20, 30]
print(40 not in numbers)
print(20 not in numbers)
True
False
not in is simply the opposite of
in.
Membership with Strings
Membership operators can also be used with strings. Python checks whether one string occurs inside another string.
text = "Python Programming"
print("Python" in text)
print("Programming" in text)
print("Java" in text)
print("Java" not in text)
True
True
False
True
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.
Membership with Lists
Lists are one of the most common places where membership operators are used.
students = ["Ali", "Sara", "Ahmed", "Fatima"]
print("Sara" in students)
print("Hamza" in students)
print("Ahmed" not in students)
True
False
False
This becomes particularly useful when validating user selections or checking whether an item already exists.
Membership with Tuples
Membership operators work with tuples in the same way they work with lists.
days = ("Monday", "Tuesday", "Wednesday")
print("Monday" in days)
print("Friday" in days)
print("Sunday" not in days)
True
False
True
Membership with Sets
Sets are especially useful when you frequently need membership checks.
numbers = {10, 20, 30, 40, 50}
print(30 in numbers)
print(100 in numbers)
print(100 not in numbers)
True
False
True
Because sets are designed around unique elements and membership operations, they are often useful for fast existence checks.
Membership with Dictionaries
Dictionaries require special attention.
When you use in with a dictionary,
Python checks the keys by default.
student = {
"name": "Ali",
"age": 25,
"city": "Multan"
}
print("name" in student)
print("age" in student)
print("Ali" in student)
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().
student = {
"name": "Ali",
"age": 25
}
print("Ali" in student.values())
print(25 in student.values())
True
True
Practical Membership Examples
Membership operators become powerful when combined with conditions.
Checking an Allowed Role
allowed_roles = ["admin", "manager", "editor"]
role = "manager"
if role in allowed_roles:
print("Access granted")
else:
print("Access denied")
Access granted
Checking an Email Domain
email = "student@example.com"
if "@example.com" in email:
print("Valid example domain")
else:
print("Different domain")
Checking a Product Category
categories = ["shoes", "shirts", "bags"]
product_category = "shoes"
if product_category in categories:
print("Category available")
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.
a = [1, 2, 3]
b = a
print(a is b)
True
Both a and b refer to the same
list object.
The is not Operator
The is not operator checks whether two
variables refer to different objects.
a = [1, 2, 3]
b = [1, 2, 3]
print(a is not b)
True
Although the lists contain the same values, they are two separate list objects.
is vs ==
This is one of the most important distinctions in Python.
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b)
print(a is b)
True
False
The Key Difference
== checks whether two values are equal.
is checks whether two variables refer
to the same object.
a = [1, 2, 3]
b = a
print(a == b)
print(a is b)
True
True
Identity and Object References
To understand is, it helps to understand
that variables in Python refer to objects.
numbers = [10, 20, 30]
another = numbers
print(numbers is another)
True
The statement:
another = numbers
does not create a new list. Both variable names refer to the same list object.
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.