Difference Between == and is in Python (Identity vs Equality)
== compares values, is compares identity (same object in memory). Learn the small-integer caching trap that surprises even experienced developers.
The one-line answer
== compares VALUES (equality); is compares IDENTITY (whether both names point to the exact same object in memory).a = [1, 2, 3]
b = [1, 2, 3]
print(a == b) # True : same values
print(a is b) # False : two different objects in memory
print(id(a), id(b)) # different addresses
When is returns True
a = [1, 2, 3]
c = a # c points to the SAME object, no copy
print(a is c) # True: same object
c.append(4)
print(a) # a also shows [1,2,3,4] - proof it's one object
The small-integer caching trap
x = 256
y = 256
print(x is y) # True - Python caches integers -5 to 256
x = 257
y = 257
print(x is y) # False (usually) - beyond the cache range!
is "accidentally" works for small numbers. Beyond that range, equal numbers can be different objects. Lesson: never use is to compare numbers or strings — always ==. This trap is a favourite in interviews.The ONE correct use of is
result = None
if result is None: # correct, Pythonic
print("No data")
if result == None: # works but wrong style (PEP 8)
print("No data")
None is a singleton — only one None object exists in the whole program — so identity check is both correct and fastest. PEP 8 officially says: comparisons to None should always be done with is or is not.
Summary table
| Point | == | is |
|---|---|---|
| Compares | Values | Object identity (memory) |
| Internally uses | __eq__() method | id() comparison |
| Use for | Numbers, strings, lists — all data | None, True/False, same-object checks |
| Can be overloaded | Yes (classes can define __eq__) | No, never |
One-line answer
== VALUES compare करता है (equality); is IDENTITY compare करता है (क्या दोनों names memory में exact same object को point कर रहे हैं).a = [1, 2, 3]
b = [1, 2, 3]
print(a == b) # True : same values
print(a is b) # False : memory में दो अलग objects
print(id(a), id(b)) # अलग addresses
is कब True देता है
a = [1, 2, 3]
c = a # c SAME object को point करता है, copy नहीं
print(a is c) # True: same object
c.append(4)
print(a) # a भी [1,2,3,4] दिखाता है - proof कि एक ही object है
Small-integer caching trap
x = 256
y = 256
print(x is y) # True - Python -5 से 256 तक integers cache करता है
x = 257
y = 257
print(x is y) # False (usually) - cache range के बाहर!
is "गलती से" काम कर जाता है. उस range के बाहर, equal numbers अलग objects हो सकते हैं. सबक: numbers या strings compare करने के लिए कभी is use न करें — हमेशा ==. यह trap interviews का favourite है.is का एकमात्र सही use
result = None
if result is None: # सही, Pythonic
print("No data")
if result == None: # चलता है पर गलत style (PEP 8)
print("No data")
None singleton है — पूरे program में सिर्फ एक None object होता है — इसलिए identity check सही भी है और सबसे तेज़ भी. PEP 8 officially कहता है: None से comparison हमेशा is या is not से करें.
Summary table
| Point | == | is |
|---|---|---|
| Compare करता है | Values | Object identity (memory) |
| Internally use करता है | __eq__() method | id() comparison |
| किसके लिए | Numbers, strings, lists — सारा data | None, True/False, same-object checks |
| Overload हो सकता है | हां (classes __eq__ define कर सकती हैं) | नहीं, कभी नहीं |
Frequently Asked Questions
What is the difference between == and is in Python?
== compares values using the __eq__ method, while is compares object identity — whether both names refer to the exact same object in memory.
Why does 256 is 256 give True but 257 is 257 give False?
Python caches small integers from -5 to 256 and reuses the same objects; numbers outside this range may be created as separate objects.
When should we use is instead of ==?
Use is only for singleton comparisons like x is None, x is True, or to check if two names point to the same object — never for comparing data values.