Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python int Type: Arbitrary Precision and Its Costs

Python's int type is arbitrary-precision, so arithmetic does not overflow like fixed-width C integers. Learn how large values affect memory and performance, plus conversion and serialization pitfalls.

~4 min readPython
#python int#arbitrary precision#big integers#numeric types
Illustration of a Python integer value growing in size with expanding digit storage, representing arbitrary precision.

Python's int type is not a fixed-width integer. A value like 2**100 is a perfectly ordinary int, and arithmetic on it behaves the same way as arithmetic on small values. This arbitrary-precision design removes an entire class of overflow bugs that C and Java developers must handle, but it introduces costs that only appear when values grow large. Understanding Python's int type means understanding both what it gives you and what it can cost you.

What Python's int Type Is Under the Hood

CPython's int is a variable-length object. Internally, it stores a sign and a sequence of digits; with the common 64-bit build, each digit stores 30 bits, while some builds use 15-bit digits. The number of digits grows with the magnitude of the value. This is why Python 3 has a single int type where Python 2 had both int and long: the unified type is always arbitrary precision.

The practical consequence is that sys.maxsize is not a limit on the value of an int. It is the maximum value a Py_ssize_t can hold, which matters for list indices and container sizes, not for integer arithmetic.

python
import sys print(sys.maxsize) # 9223372036854775807 on 64-bit print(10**100 + 1) # works fine, no overflow

Overflow Is Not an Error in Python

In C, signed integer overflow is undefined behavior. In Java, it wraps silently. In Python, it simply does not happen. Adding two large int values produces a larger int.

python
a = 10**30 b = 10**30 print(a + b) # 2000000000000000000000000000000

This convenience changes how you handle validation. A value read from an API response can be arbitrarily large, and if the downstream system has a fixed-width limit, you must check the bounds yourself.

When Large Integers Cost More Than You Expect

Arithmetic on small int values is fast because the value fits in a single internal digit. For larger values, addition and subtraction take time proportional to the number of digits. Multiplication is worse and becomes superlinear for very large operands: multiplying two thousand-digit numbers is noticeably slower than multiplying two small numbers.

Memory also grows with magnitude. Each int object carries object overhead — in CPython, typically around 28 bytes for a small value on a 64-bit build — plus additional bytes per digit. A million-digit integer consumes on the order of hundreds of kilobytes. If you construct such values repeatedly in a loop, allocation cost becomes a real concern.

python
# Constructing a million-digit integer huge = 10**1_000_000

As a one-off this is fine. In a hot path, it is not something to do repeatedly without measuring.

Mixing int with float and Decimal

When you mix int and float, the int is converted to float first. Because float has only 53 bits of mantissa, precision is lost for large values.

python
big = 2**53 print(big + 0.5) # 9007199254740992.0, not 9007199254740992.5

If exact arithmetic matters, keep everything as int or use decimal.Decimal.

Division is a frequent source of confusion. For int operands, the / operator returns a float, while // returns an int.

python
print(7 / 2) # 3.5 print(7 // 2) # 3

Converting Strings, Bytes, and Other Types to int

The int() constructor accepts strings, bytes, and bytearrays, with an optional base argument.

python
int("ff", 16) # 255 int(b"101", 2) # 5 int(" 42 ") # 42, surrounding whitespace is stripped

Invalid input raises ValueError; a type that does not support conversion raises TypeError. When parsing untrusted input, catch ValueError explicitly rather than letting it propagate as an unhandled exception.

python
try: value = int(user_input) except ValueError: value = None

Small Integer Caching and the is Trap

CPython caches small integers from -5 through 256, so code like a = 256; b = 256 can make a is b true. For values outside that range, identity may be true in some compiled contexts and false in others, depending on constant reuse and the Python implementation. This behavior is an implementation detail, not a language guarantee.

python
a = 256 b = 256 print(a is b) # True in CPython for this code c = 257 d = 257 print(c is d) # implementation detail: may be True or False

Always use == for value comparison. Relying on the cache behavior for identity checks will break in subtle ways and is not portable across Python implementations.

Practical Guidance for int in Production Code

When validating input that will be passed to a system with fixed-width limits, check bounds explicitly.

python
MAX_SAFE = 2**63 - 1 value = int(user_input) if value > MAX_SAFE: raise ValueError("value exceeds downstream limit")

For performance-sensitive numeric code, keep values small when possible. If heavy arithmetic on huge numbers is unavoidable, consider whether the algorithm can be restructured to avoid them.

For serialization, JSON itself does not define a maximum precision, but many JSON consumers use IEEE-754 double precision for numbers, and some use fixed-width integer limits. Python's json module can serialize large int values as numeric literals, but unless the receiving system also handles arbitrary-precision integers, a string is the safer interchange format.

python
import json payload = json.dumps({"value": str(10**40)})

The receiving side can parse the string back to an integer, avoiding the silent rounding that a JSON number could cause.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.