In Python, == and is are two different tests:
-
==asks: do these two objects have the same VALUE? -
isasks: are these literally the SAME object in memory?
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b) # True -- same value
print(a is b) # False -- different objects
Why it fools beginners: CPython caches small integers and short strings, so 256 is 256 can be True while 257 is 257 is False. Your code "works" in the small test and breaks in production.
THE RULE: Use == for values. Reserve is for None, True and False — never for numbers or text.
Fennec Teaches Python makes 60-second videos on Python mistakes like this one. All 18 fixes are collected in the Python Mistakes Pack: https://fennecteachespython.gumroad.com/l/yiokvw
Top comments (0)