Equality (==) vs Identity (is)
"There should be one—and preferably only one—obvious way to do it."
— The Zen of Python
Introduction
Python provides two different ways to compare objects:
==checks whether two objects have the same value.ischecks whether two variables refer to the exact same object in memory.
Understanding this distinction is essential for writing correct Python code and answering common interview questions.
Equality (==)
The equality operator compares values.
Output
Although a and b are different objects, they contain the same values.
Identity (is)
The identity operator compares object identity.
Output
Two separate list objects were created.
Visual Comparison
flowchart LR
a --> L1["[1, 2, 3]"]
b --> L2["[1, 2, 3]"]
a == b→ ✅Truea is b→ ❌False
The values are equal, but the objects are different.
Shared References
flowchart LR
a --> L["[1, 2, 3]"]
b --> L
Now
returns
and
also returns
because both variables reference the same object.
The id() Function
Every Python object has an identity.
You can inspect it using id().
Example output
The IDs are identical because both variables reference the same object.
Different objects have different IDs.
The IDs will differ.
Why None Should Always Use is
The recommended way to compare with None is:
Not
Why?
None is a singleton object.
There is only one None instance in a Python program.
Identity comparison is both faster and semantically correct.
Integer Caching
Python caches small integers to improve performance.
Example
Often outputs
Now try
Depending on the Python implementation and execution context, this may return either True or False.
Never rely on is when comparing numbers.
Use ==.
String Interning
Python may also reuse identical string objects.
This may return
because Python often interns strings.
However, this is an implementation optimization.
Do not rely on it.
Always compare strings using
Custom Objects
Consider
returns
because two different objects were created.
Likewise,
also returns
When to Use ==
Use == when comparing:
- Numbers
- Strings
- Lists
- Tuples
- Dictionaries
- Sets
- Custom values
Example
When to Use is
Use is for:
NoneTrueandFalse(rarely needed)- Singleton objects
- Explicit identity checks
Example
Time Complexity
Both operations are generally O(1).
However:
==may compare multiple elements (for example, two long lists), making it O(n) in the worst case.isonly compares object identity and is always O(1).
Common Interview Problems
Understanding equality and identity is useful for:
- Linked Lists
- Graphs
- Trees
- Memoization
- Object-Oriented Design
- Caching
- Singleton Pattern
Common Mistakes
Mistake 1
Using
Instead use
Mistake 2
Comparing strings using
Always use
Mistake 3
Assuming equal objects share memory.
does not mean
Mistake 4
Relying on integer caching.
Never write code that depends on
or
Always compare numeric values with ==.
Best Practices
- Use
==to compare values. - Use
isto compare identity. - Compare with
Noneusingis. - Do not rely on Python's object caching.
- Use
id()only for debugging and learning.
Key Takeaways
==compares values.iscompares object identity.- Two equal objects may not be the same object.
Noneshould always be compared usingis.- Integer and string caching are implementation optimizations and should not influence your code.
Related Topics
- Variables, Objects & References
- Python Memory Model
- Mutable vs Immutable Objects
- Assignment vs Copying
Practice Questions
- What is the difference between
==andis? - Why should
Nonebe compared usingis? - What does
id()return? - Why shouldn't you rely on integer caching?
- Can two different objects be equal?
Summary
Python distinguishes between value equality and object identity.
Understanding the difference helps you avoid subtle bugs and write clearer, more idiomatic Python.
As a rule of thumb:
- Use
==when comparing values. - Use
iswhen checking identity, especially withNone.
This distinction is simple, but it appears regularly in technical interviews and production code.