🧠 What's the Difference Between is and == in Python?


If you've ever compared objects in Python and felt unsure about whether to use is
or ==
, you're not alone. This is a common question — even among experienced developers. The answer lies in understanding how Python handles object identity and equality.
This is the sixth article in my Python fundamentals series, and here we'll definitively clarify the difference between is
and ==
, with practical examples, common pitfalls, and clear guidelines on when to use each.
🧪 ==
checks for value equality
The ==
operator compares values. It checks whether the compared objects have the same content, even if they are stored in different memory locations.
Example:
x = [1, 2, 3]
y = [1, 2, 3]
print(x == y) # True
Even though x
and y
are different list objects (different addresses), their contents are the same. So ==
returns True
.
🧬 is
checks for object identity
The is
operator compares whether two variables point to the same object in memory. In other words, whether both names are actually references to the exact same object.
Example:
x = [1, 2, 3]
y = x
print(x is y) # True
Here, y
is a direct reference to the same object as x
. So is
returns True
.
Now see this case:
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b) # True
print(a is b) # False
The contents are equal, but the objects are different.
⚠️ Gotchas with immutable types
To optimize performance, Python reuses some small immutable objects like integers and short strings. This can be confusing:
a = 256
b = 256
print(a is b) # True (same object)
x = 1000
y = 1000
print(x is y) # False (different objects)
With strings:
s1 = "python"
s2 = "python"
print(s1 is s2) # True (interned)
s3 = "py" + "thon"
print(s3 is s2) # May be True or False, depending on interpreter optimizations
⚠️ Avoid using is
to compare strings, numbers, or any immutable types. Always use ==
in those cases.
🧠 When should you use is
?
Use is
when you want to check whether two variables refer to the exact same object. Common cases:
- Checking if something is
None
:
if x is None:
...
- Identity comparisons for special objects (e.g. singletons)
✅ When should you use ==
?
Use ==
when you want to compare values:
Numbers, strings, lists, dictionaries, or custom objects with
__eq__
When you care about the content, not the identity
🧪 Practical example with a class
class Person:
def __init__(self, name):
self.name = name
def __eq__(self, other):
return isinstance(other, Person) and self.name == other.name
p1 = Person("Ana")
p2 = Person("Ana")
print(p1 == p2) # True (same content)
print(p1 is p2) # False (different objects)
📌 Conclusion
==
→ compares values (content)is
→ compares identity (same object)
Use ==
in most situations. Use is
when checking for None
, singletons, or when you specifically want to check if two variables point to the same memory location.
Knowing this difference helps you avoid subtle bugs — especially when working with mutable objects.
#Python #Comparisons #PythonTips #EqualsVsIs #PythonFundamentals #DevLife #CleanCode
Subscribe to my newsletter
Read articles from Bruno Marques directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
