When working with Python, you will often see the is and == operators used for comparisons.
At first, they may look similar, but they are designed for completely different purposes.
The key difference is:
-
==checks whether two objects have the same value. -
ischecks whether two variables refer to the same object in memory.
Understanding this difference is important because using the wrong operator can lead to unexpected behavior, especially when working with objects, APIs, and complex applications.
The == Operator: Comparing Values
The == operator is used for equality comparison.
It checks whether two objects contain the same data.
Example:
numbers_a = [1, 2, 3]
numbers_b = [1, 2, 3]
print(numbers_a == numbers_b)
Output:
True
Although numbers_a and numbers_b are two separate lists, their contents are identical, so == returns True.
Python compares the values stored inside the objects.
The is Operator: Comparing Object Identity
The is operator works differently.
Instead of checking values, it checks whether two variables point to the exact same object.
Example:
numbers_a = [1, 2, 3]
numbers_b = numbers_a
print(numbers_a is numbers_b)
Output:
True
Here, both variables reference the same list object.
You can think of it like this:
numbers_a ───┐
├── [1, 2, 3]
numbers_b ───┘
Both names point to the same location in memory.
Now consider this example:
numbers_a = [1, 2, 3]
numbers_b = [1, 2, 3]
print(numbers_a == numbers_b)
print(numbers_a is numbers_b)
Output:
True
False
Why?
Because the values are equal, but the objects are different.
Memory representation:
numbers_a ─── [1, 2, 3]
numbers_b ─── [1, 2, 3]
Two different objects with the same content.
Why Do We Use is None Instead of == None?
One of the most common places where Python developers use is is checking for None.
Example:
result = None
if result is None:
print("No result available")
Output:
No result available
This is the recommended Python style.
You may also see:
if result == None:
print("No result available")
Although it works in many cases, it is not the preferred approach.
The reason is that None is a singleton object in Python.
A singleton means Python creates only one None object, and every reference to None points to that same object.
Therefore:
result is None
means:
"Is this variable pointing to the actual None object?"
This is exactly what we want.
A Common Mistake: Using is With Numbers
A common beginner mistake is using is to compare numbers.
Example:
a = 10
b = 10
print(a is b)
You may get:
True
But this does not mean is should be used for numbers.
Python internally caches some small integer objects. Because of this optimization, two variables may reference the same object.
However, this behavior is an implementation detail and should not be relied on.
Example:
a = 10000
b = 10000
print(a is b)
The result can be different depending on how Python creates these objects.
The correct way:
a == b
Because we want to compare the values, not the objects.
is With Strings
The same concept applies to strings.
Example:
a = "python"
b = "python"
print(a == b)
print(a is b)
Output:
True
True
You might see True for both because Python can reuse some immutable objects through a process called interning.
But again, this is not something you should depend on.
Always use:
a == b
when comparing string values.
How Python Thinks About Variables
In Python, variables are not containers that store values directly.
They are references to objects.
Example:
name = "Python"
Conceptually:
name ───> "Python"
When you create another reference:
another_name = name
You get:
name ─────────┐
├── "Python"
another_name ─┘
Both variables point to the same object.
This is why is can tell whether two variables refer to the same object.
Quick Comparison Table
| Operator | Purpose | Example |
|---|---|---|
== |
Compare values | age == 30 |
is |
Compare object identity | user is admin |
is None |
Check if value is None | result is None |
Best Practices
Use == when:
- Comparing numbers
- Comparing strings
- Comparing lists, dictionaries, or other data
- Checking if two values are equal
Example:
if username == "admin":
print("Welcome")
Use is when:
- Checking singleton objects
- Checking
None - Checking identity between objects
Example:
if response is None:
return
Final Thoughts
The difference between is and == comes down to one simple idea:
-
==asks: "Are these objects equal?" -
isasks: "Are these the same object?"
For everyday comparisons, == is usually the right choice.
Use is when you specifically care about object identity, especially when checking for None.
Understanding this distinction will help you write cleaner and more predictable Python code.
Top comments (0)