Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
Free tutorials in Hindi & English Daily computer, mobile and IT guides Beginner friendly learning
Blog · Python · 04 Jul 2026 · Hindi + English

Python में == और is में अंतर (Identity vs Equality)

== values compare करता है, is identity compare करता है (memory में same object). Small-integer caching trap सीखें जो experienced developers को भी surprise करता है.

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
True False 140234... 140235...

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
True [1, 2, 3, 4]

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!
True False
Python pre-creates integers from -5 to 256 and reuses them, so 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")
No data 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
ComparesValuesObject identity (memory)
Internally uses__eq__() methodid() comparison
Use forNumbers, strings, lists — all dataNone, True/False, same-object checks
Can be overloadedYes (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
True False 140234... 140235...

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 है
True [1, 2, 3, 4]

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 के बाहर!
True False
Python -5 से 256 तक के integers पहले से बनाकर reuse करता है, इसलिए छोटे numbers पर 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")
No data No data

None singleton है — पूरे program में सिर्फ एक None object होता है — इसलिए identity check सही भी है और सबसे तेज़ भी. PEP 8 officially कहता है: None से comparison हमेशा is या is not से करें.

Summary table

Point==is
Compare करता हैValuesObject identity (memory)
Internally use करता है__eq__() methodid() comparison
किसके लिएNumbers, strings, lists — सारा dataNone, True/False, same-object checks
Overload हो सकता हैहां (classes __eq__ define कर सकती हैं)नहीं, कभी नहीं

Frequently Asked Questions

Python में == और is में क्या अंतर है?

== values को __eq__ method से compare करता है, जबकि is object identity compare करता है — क्या दोनों names memory में exact same object को refer करते हैं.

256 is 256 True क्यों देता है लेकिन 257 is 257 False?

Python -5 से 256 तक के integers cache करके same objects reuse करता है; इस range के बाहर के numbers अलग objects के रूप में बन सकते हैं.

is को == की जगह कब use करना चाहिए?

is सिर्फ singleton comparisons के लिए use करें जैसे x is None, x is True, या यह check करने के लिए कि दो names same object को point करते हैं — data values compare करने के लिए कभी नहीं.