What is the difference between `is` and `==` in Python?
β‘ Short Answer
`==` compares values (are they equal?); `is` compares identity (are they the exact same object in memory?). Use `==` for value checks and reserve `is` for singletons like `None`, `True`, `False`. Two equal objects can be different objects, so `==` can be True while `is` is False.
βCoffee Chat Question
Concept Made Simple
βWhat is the difference between `is` and `==` in Python?β
π§ Mind Map Answer
Remember It Faster
β¨οΈHands-on Keyboard
Learn by Doing
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b) # True - same values
print(a is b) # False - different objects
x = None
print(x is None) # correct None checkTrue False True
π₯What If?
Think Beyond the Expected
Why does `a is b` sometimes return True for small integers but not large ones?
CPython caches (interns) small integers (-5 to 256) and short strings, so identical literals may share one object β making `is` accidentally True. That's an implementation detail, not a guarantee. Never rely on `is` for value comparison; always use `==`.
πReal World
The classic bug is `if x is 0` or `if name is 'admin'` β it works in tests thanks to interning, then fails in production on a computed value. Linters flag `is` with literals for exactly this reason.
π―Interviewer's Expectation
Keywords they're listening for:
β οΈCommon Mistakes
- βUsing `is` to compare values/literals
- βWriting `== None` instead of `is None`
- βRelying on small-int caching behavior
β Best Practices
- βUse `==` for value equality
- βUse `is` only for None/True/False/singletons
- βLet linters catch `is` with literals
πFollow-up Questions
- 1Why is `is None` preferred over `== None`?
- 2What is string/int interning?
- 3How does overriding __eq__ affect ==?
π§©Related Technologies
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly β AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Data Types (Python) Interview question: "What is the difference between `is` and `==` in Python?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
β Featured Products
Support our platform by exploring our recommended products.
As an Amazon affiliate, purchases through these links may earn us a small commission β at no extra cost to you. It helps keep Full Stack Interview Guru free.