Python Identity Operators (is, is not)
What Are Identity Operators in Python?
Identity operators are keywords in Python that compare the memory locations of two objects. They check if two variables refer to the exact same object instance. This means you can tell if two variables are not just equal in value but are actually the same object.
For example, two different lists can contain the same elements but still be two separate objects in memory.
Want to master Python? This lesson is part of our Free Python course covering operators, memory, objects and more.
Explore this free Python course →The "is" Operator
The is operator returns True if both variables point to the exact same object in memory. If they point to different objects (even if their content is identical), it returns False. Think of it as asking, "Do these two variables refer to the same thing?"
Here's how it works with a list:
list_a = [1, 2, 3]
list_b = [1, 2, 3]
list_c = list_a
print(list_a is list_b)
print(list_a is list_c)
Your output will appear here...
In this example, list_a and list_b have identical content but are separate objects, so is returns False. However, list_c was assigned directly from list_a, so they both point to the same object, and is returns True.
The "is not" Operator
The is not operator is the opposite of the is operator. It returns True if both variables point to different objects in memory. If they point to the same object, it returns False. This operator asks, "Are these two variables different objects?"
Let's use the same lists from the previous example:
list_a = [1, 2, 3]
list_b = [1, 2, 3]
list_c = list_a
print(list_a is not list_b)
print(list_a is not list_c)
Your output will appear here...
Since list_a and list_b are separate objects, is not correctly returns True. Because list_a and list_c refer to the same object, is not returns False.
Identity vs. Equality: is vs. ==
A common source of confusion is the difference between the is operator and the == operator. While is checks for object identity, == checks for value equality. This is a critical distinction in Python.
is(Identity): Checks if two variables point to the same memory location.==(Equality): Checks if the values of the objects are the same.
Let's revisit our list example to see the difference clearly.
list_a = [1, 2, 3]
list_b = [1, 2, 3]
print(f"Identity check (is): {list_a is list_b}")
print(f"Equality check (==): {list_a == list_b}")
Your output will appear here...
The is operator returns False because list_a and list_b are two separate list objects stored in different memory locations. The == operator returns True because the content of both lists is identical.
When to Use Identity Operators
Use identity operators in specific situations where you need to check for object identity, not value. The most common and recommended use cases are:
- Check for
None: Useisto check if a variable isNone. BecauseNoneis a singleton object (only one instance exists),isis both faster and more explicit. - Compare to singletons: Use
iswhen comparing to other singletons likeTrueandFalse. For example,if my_variable is True:. - Verify specific object instances: Use it when your program's logic depends on knowing if you're working with the exact same object instance.
def my_function(value):
if value is None:
print("No value was provided.")
else:
print(f"The value is {value}.")
my_function("hello")
my_function(None)
Your output will appear here...
You're making great progress! Ready to practice with real-world problems?
Python ExercisesA Common Pitfall: Python's Caching Behavior
You might see situations where is returns True unexpectedly, especially with numbers and short strings. This happens because Python caches (or "interns") certain immutable objects for performance reasons.
For example, Python pre-allocates integer objects from -5 to 256. When you create an integer in this range, Python points the variable to the existing cached object instead of creating a new one.
# Integers in the cached range (-5 to 256)
a = 256
b = 256
print(a is b)
# Integers outside the cached range
x = 257
y = 257
print(x is y)
Your output will appear here...
This behavior is an implementation detail of the CPython interpreter and can change. Never rely on it. Always use == to compare numerical values. This shows why it's crucial to use is only for true identity checks, like with None.
Final Challenge: The Clone Checker
Prove your understanding of is versus ==.
Your Task:
- Create two lists,
xandy, that both contain[10, 20]. - Create a variable
zand assignxto it (z = x). - Check if
xequalsy(Value equality). - Check if
xisy(Object identity). - Check if
xisz(Object identity).
x = [10, 20]
y = [10, 20]
z = x
print("x == y:", x == y)
print("x is y:", x is y)
print("x is z:", x is z)
Your output will appear here...
Lesson Completed
You have successfully learned about Python Identity Operators, the difference between equality and identity, and Python's object caching.
Full Python Course
Master Python with 11+ hours of content, 50+ exercises, and real-world projects.
Enroll Now