InterviewHubPRO
Section Overview
Q9

Question 9 of 65

Part 2: Python Core Language, Internals & Runtime (6 - 25)

14%
Python & FastAPI•Part 2: Python Core Language, Internals & Runtime (6 - 25)

`is` vs `==` and Integer Caching:

In Simple Words:

  • ==: Compares values (a == b).
  • is: Compares memory identity (id(a) == id(b)).
  • Small Integer Caching: CPython pre-allocates integers between -5 and 256, so a = 100; b = 100; a is b is True.
python
a = 256
b = 256
print(a is b) # True (cached by CPython runtime)

x = 1000
y = 1000
print(x == y) # True (same values)
print(x is y) # False (different objects in memory)