Difference Between is and == in Python
Learn the difference between is and == in Python: identity vs value comparison, why to use 'is None', interning caching gotchas, and clear examples.
Expected Interview Answer
== compares whether two objects have equal values, while is compares whether two names refer to the exact same object in memory (identity).
The == operator calls the object's __eq__ method, so it can be customised and returns True whenever the values are considered equal. The is operator checks object identity, equivalent to comparing id(a) == id(b); it is never overridable. Because small integers and interned strings are cached, is may accidentally appear to work for them, but you should still use == for value comparison and reserve is for singletons like None, True, and False.
- == gives meaningful value equality across different objects
- is gives fast, unambiguous identity checks
- is None is the correct, reliable way to test for None
- Understanding both prevents subtle caching-related bugs
- == can be customised via __eq__ for your own classes
AI Mentor Explanation
Two bats can be identical models with the same weight and grip — that is == , equal in value. But is asks whether it is literally the very same bat in your hands, the one physical object. Two players holding matching bats are equal, yet they are not the same bat.
Step-by-Step Explanation
Step 1
Compare values with ==
== invokes __eq__ and returns True when the two objects are considered equal in value.
Step 2
Compare identity with is
is returns True only when both names reference the exact same object, like id(a) == id(b).
Step 3
Test singletons with is
Use x is None, x is True, x is False — there is only ever one of each in memory.
Step 4
Beware caching
Small ints (-5..256) and interned strings are cached, so is may look equal for them by accident.
Step 5
Choose deliberately
Use == for value checks and is only for identity or singletons.
What Interviewer Expects
- A clear value-vs-identity distinction
- Knowledge that == calls __eq__ and is compares id()
- The rule to use is None rather than == None
- Awareness of integer and string interning caching effects
- Understanding that is cannot be overridden
Common Mistakes
- Using is to compare strings or numbers for equality
- Writing x == None instead of x is None
- Assuming is always works for small integers because of caching
- Believing == and is are interchangeable
- Thinking is can be customised like __eq__
Best Answer (HR Friendly)
“== checks whether two things have the same value, while is checks whether they are literally the same object in memory. In everyday code you compare values with ==, and you use is mainly to check for None.”
Code Example
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a == b) # True -> same values
print(a is b) # False -> different objects in memory
print(a is c) # True -> c refers to the same object as a
# The correct way to check for None
x = None
print(x is None) # True (preferred)
# Caching can be misleading -- do NOT rely on it
print(256 is 256) # True (small ints are cached)
print(1000 is 1000) # may be False; use == for value checksFollow-up Questions
- Why should you use 'is None' instead of '== None'?
- What is integer interning and why can it make 'is' look correct?
- Can you override the behaviour of the is operator?
- How does == relate to the __eq__ method?
- When are two string literals guaranteed to be the same object?
MCQ Practice
1. What does the is operator compare?
is checks whether two references point to the exact same object, equivalent to comparing id().
2. Which is the recommended way to check for None?
None is a singleton, so identity comparison with is None is correct and reliable.
3. For a = [1,2,3] and b = [1,2,3], what are a == b and a is b?
The lists have equal values (==) but are separate objects, so is returns False.
Flash Cards
What does == compare? — Value equality, by calling the object's __eq__ method.
What does is compare? — Object identity — whether both names point to the same object.
How should you check for None? — Use 'x is None', never '== None'.
Why can is look right for small ints? — CPython caches integers from -5 to 256, so they are the same object.
Can is be overridden? — No — unlike ==, the is operator's behaviour is fixed.