r/learnpython • u/developer-mind • 2d ago
`is` vs `==` in Python: why does `a is b` sometimes work on numbers and then break?
I was going through basic Python interview questions this week and this one keeps tripping people up, me included when I first learned it.
```python
a = 256
b = 256
print(a is b) # True
x = 1000
y = 1000
print(x is y) # often False when run as separate statements
print(x == y) # True
```
What I understand so far: `==` asks "do these two have the same value?" and `is` asks "are these literally the same object in memory?" CPython caches small ints (-5 to 256), so those happen to share one object. Bigger numbers can be separate objects, so `is` gives False even though the values match.
So the rule I follow now: `==` for values, `is` only for `None` (`if x is None`).
Is there any other case where you'd actually reach for `is`? Or any gotcha I'm missing, like string interning? Not sure I fully get that part.