Skip to main content
← Back to Code Arena
Medium Apprentice SpeedRun identity equality cpython-internals

Python: `is` vs `==` and the 256 Cache

+180 XP · 5 min · attempt 1

Problem

In CPython, run each of these in a fresh REPL session:

    a = 256;  b = 256;  print(a is b)        # line A
    a = 257;  b = 257;  print(a is b)        # line B
    a = -5;   b = -5;   print(a is b)        # line C
    a = 1000; b = 1000; print(a is b)        # line D

How many lines print `True`? Enter a single integer 0-4.

(Assume each line is run as separate top-level statements at the REPL, NOT inside a function or compiled together.)

Starter

# CPython caches small ints from -5 to 256 inclusive.
# Outside that range, `is` may be False even though `==` is True.

Your answer

Playing as a guest — you'll be asked for a display name on submit. Log in to keep your XP across devices.