bigonotes
DSA: Patterns & PythonPython Internals

Integer

How Python ints store arbitrary-size numbers, the small-int cache, what arithmetic costs, and int gotchas in interviews.

5 min readLast updated

In C and many other languages an integer is a fixed 4 or 8 bytes, and going past 263−12^{63}-1 overflows. A Python int is a full object that grows as needed, so it never overflows (until you run out of memory). That convenience costs memory and speed, and it comes with a few behaviors that trip people up in interviews. This note walks through how an int is stored, why that makes it safe and slow, and which edge cases to watch for.

How an int is stored#

"No overflow" starts with the layout. CPython stores an int as a PyLongObject, which looks roughly like this C struct:

struct PyLongObject {
    Py_ssize_t   ob_refcnt;   // reference count
    PyTypeObject *ob_type;    // points to the int type
    Py_ssize_t   ob_size;     // digit count (also holds the sign)
    digit        ob_digit[1]; // the digits, least significant first
};

(Python 3.12 packs size and sign into one tag field, but the idea is the same.)

The number lives in ob_digit as an array of "super-digits" in base 2302^{30} on 64-bit builds. Each digit holds a value from 0 up to about one billion. Here's how a few values map onto that array:

Value ob_digit Digits used
1789 [1789] 1
2**30 + 5 [5, 1] (value =5+1⋅230= 5 + 1 \cdot 2^{30}) 2
2**1000 34 digits 34

Big numbers use a longer digit array. That's the whole trick behind "no overflow".

The small-integer cache#

Since every int is a full object, creating a fresh one for every 0, 1 or -1 would be wasteful. So CPython creates the integers -5 to 256 once at startup and reuses them.

Integers -5 to 256 live in a shared cache, so two names set to 100 point at the same object, while two names set to 300 get separate objects

You can see the cache with is, which checks whether two names point at the same object:

a = 256
b = 256
print(a is b)                  # True: both point at the cached 256

c = int("257")                 # built at runtime, outside the cache
d = int("257")
print(c is d)                  # False: two separate objects
print(c == d)                  # True: same value

If you type c = 257; d = 257 in one script, c is d may print True, because the compiler can reuse one constant within the same code block. That is an implementation detail. Always compare numbers with ==, never is.

Since Python 3.12, cached ints are "immortal": their refcount is never decremented, so they are never freed.

Ints are immutable#

The cache is safe to share because ints can't change. Arithmetic always produces a result object, and the name moves to it, as id() shows here:

x = 1789
before = id(x)
x = x + 1
print(id(x) == before)   # False: x now names a different object

Here's what x = x + 1 does step by step:

  1. Read the C value out of the 1789 object and add 1 (plain C arithmetic).
  2. If the result is in -5..256, return the cached object. Otherwise allocate a new int object holding 1790.
  3. Point x at the result.
  4. Decrement the old object's refcount. If nothing else uses 1789, it is freed.

Whether step 2 allocates depends on the result, as these examples show:

Expression New object allocated? Why
2 + 3 No 5 comes from the cache
1000 + 2000 Yes 3000 is outside the cache
my_list.append(5) No new list Lists are mutable, same object grows

What ints cost#

Being a full object makes an int flexible, and you pay for that in two ways.

Memory. A small int takes about 28 bytes (header + size + one digit), versus 4 bytes in C. You can check it with sys.getsizeof:

import sys
print(sys.getsizeof(1))        # 28 on a typical 64-bit build
print(sys.getsizeof(2**30))    # 32: needs a second digit

A list of one million distinct large ints is roughly 28 MB of int objects plus 8 MB of list pointers, not the 4 MB you'd expect in C. If memory matters, array.array('i', ...) stores raw C ints.

Time. Operations on small ints are effectively O(1)O(1). On huge ints they scale with the number of digits dd:

Operation Cost on big ints
+, -, comparison O(d)O(d), like column addition on paper
* Karatsuba for large operands, about O(d1.58)O(d^{1.58})
str(n) / int(s) Superlinear; Python 3.11+ refuses over 4300 digits by default (sys.set_int_max_str_digits)

So "arithmetic is O(1)O(1)" is a fair assumption in interviews only while numbers stay bounded.

Interview gotchas#

Integer behavior that differs from other languages often shows up in interview edge cases. The snippet below collects the ones that come up most: rounding of // and %, bool as an int, negative shifts, and a few built-ins.

# Floor division and modulo round toward negative infinity
print(-7 // 2)        # -4 (C gives -3)
print(int(-7 / 2))    # -3: truncates toward zero ("Evaluate RPN")
print(-7 % 3)         # 2: result takes the sign of the divisor

# bool is a subclass of int
print(True + True, isinstance(True, int))   # 2 True

# Negative numbers act like infinite two's complement
print(-1 >> 1)        # -1: right shift never reaches 0
MASK = 0xFFFFFFFF     # simulate 32-bit ints in bit problems
print((-1) & MASK)    # 4294967295

# Handy built-ins
print(int("101", 2), bin(5))    # 5 '0b101'
print((5).bit_count())          # 2 (bit_count needs 3.10+)
print(float("inf") > 10**100)   # True: a safe "infinity" sentinel

These behaviors lead to three habits:

  • No overflow means you never need max-int overflow checks, but problems that say "assume 32-bit integers" (e.g. Reverse Integer) expect you to clamp yourself.
  • Bit tricks with negatives need a mask, since Python has no fixed width.
  • Use math.isqrt(n) for exact integer square roots instead of int(n ** 0.5), which can be off by one for large n.

Key takeaways#

  • A Python int is an object holding an array of base-2302^{30} digits, so it never overflows.
  • -5 to 256 are cached singletons; compare numbers with ==, not is.
  • Ints are immutable: every arithmetic result is a (possibly cached) different object.
  • Each int costs about 28 bytes, and big-number arithmetic scales with digit count.
  • // and % floor toward negative infinity, and bool is an int.
Revision notes · 2 min

Core idea#

A Python int is an object that grows as needed, so it never overflows. That costs memory and speed, and a few behaviors differ from other languages.

Key points#

  • CPython stores an int as a PyLongObject with an array of digits in base 2302^{30}, least significant first.
  • Big numbers use a longer digit array. That is the whole "no overflow" trick.
  • CPython caches the integers -5 to 256 at startup and reuses them. Since 3.12 they are immortal.
  • Compare numbers with ==, never is.
  • Ints are immutable. x = x + 1 makes or fetches another object and moves the name.
  • A small int takes about 28 bytes, against 4 bytes in C. array.array('i', ...) stores raw C ints.

Cost on big ints#

Operation Cost with d digits
+, -, comparison O(d)O(d)
* Karatsuba, about O(d1.58)O(d^{1.58})
str(n), int(s) Superlinear; 3.11+ refuses over 4300 digits by default

Treat arithmetic as O(1)O(1) only while numbers stay bounded.

Interview gotchas#

Expression Result Note
-7 // 2 -4 Floors toward negative infinity
int(-7 / 2) -3 Truncates toward zero (Evaluate RPN)
-7 % 3 2 Takes the sign of the divisor
True + True 2 bool is a subclass of int
-1 >> 1 -1 Infinite two's complement; mask with 0xFFFFFFFF

Pitfalls#

  • Forgetting to clamp when a problem says "assume 32-bit integers" (Reverse Integer).
  • Bit tricks with negatives and no mask.
  • int(n ** 0.5) can be off by one for large n. Use math.isqrt(n).

Say it in an interview#

  • "Python ints are arbitrary precision, so there is no overflow, but big-number arithmetic scales with digit count."
  • "Floor division rounds toward negative infinity, so I use int(a / b) to truncate."

Found a mistake? Email ask@bigonotes.com