EasyπŸ‘€ 0-2 yearsπŸ‘€ 3-5 years 1 min read

What is the difference between `is` and `==` in Python?

Asked inInfosysWiproAccentureCapgemini
#is#equality#identity#none#python basics
Report issue

⚑ 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

==β†’value equality (calls __eq__)
is→same object (same id)
Rule→use `is None`, `==` for everything else

⌨️Hands-on Keyboard

Learn by Doing

python
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 check
Output
True
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:

βœ“ == value, is identityβœ“ use `is None`βœ“ equal β‰  same objectβœ“ integer/string interning caveatβœ“ don't use `is` for literals

⚠️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

id()interning__eq__

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?

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.

Related Questions