Maximum Value of int in Python (sys.maxsize Explained)

2 weeks ago 55
Club.noww.in

There’s no maximum value of int in Python 3: integers grow as large as your memory allows. What people often mean is sys.maxsize, which is 9,223,372,036,854,775,807 (2**63 – 1) on 64-bit systems, the largest size a list or string can report:

import sys sys.maxsize # 9223372036854775807 2 ** 100 + 1 # fine, no overflow

All the limits below were printed by Python 3.12.5 with NumPy 2.5.3 on 64-bit Windows, including the overflow warnings NumPy gives.

We’ll cover Python’s unlimited integers, what sys.maxsize really is, float limits, the fixed-size integers in NumPy that do overflow, and using infinity as a starting value.

Python ints have no maximum value

Your Python 3 integers have arbitrary precision. When a number outgrows a machine word, Python switches to a bigger representation automatically, so arithmetic stays exact:

import sys big = 2 ** 100 print(big) print(big * big) # still exact print(type(big).__name__, "| digits:", len(str(big * big))) print("sys.maxsize:", sys.maxsize, "=", "2**63 - 1" if sys.maxsize == 2**63 - 1 else "?") print(sys.maxsize + 1) # no overflow for Python ints

Output:

1267650600228229401496703205376 1606938044258990275541962092341162602522202993782792835301376 int | digits: 61 sys.maxsize: 9223372036854775807 = 2**63 - 1 9223372036854775808
Command Prompt output of Python computing 2 to the 100th power and its square exactly and adding 1 to sys.maxsize without overflowsys.maxsize + 1 works fine. Python ints don’t overflow.

Python 2 had separate int and long types with a real limit; Python 3 merged them. The one practical cap is converting huge ints to strings, which stops at 4,300 digits by default; see converting an int to a string.

What is sys.maxsize?

sys.maxsize is the largest value a C Py_ssize_t can hold, which bounds things like the length of a list or range. It isn’t a limit on integers themselves:

import sys print(f"sys.maxsize = {sys.maxsize:,}") print("default for an empty min():", min([], default=sys.maxsize)) items = range(sys.maxsize) # largest length a container can report print("len(range(sys.maxsize)) works:", len(items) == sys.maxsize) try: len(range(sys.maxsize + 1)) except OverflowError as err: print("OverflowError:", err)

Output:

sys.maxsize = 9,223,372,036,854,775,807 default for an empty min(): 9223372036854775807 len(range(sys.maxsize)) works: True OverflowError: Python int too large to convert to C ssize_t
Command Prompt output of Python sys.maxsize printed with commas, used as a default, and an OverflowError from a range one largerA range of sys.maxsize numbers is fine; one more and len() overflows.

You’ll often see sys.maxsize used as a stand-in for “infinitely large” in older code. I find math.inf clearer for that, as shown at the end.

What is the maximum float in Python?

Floats are different, and you’ll hit their limits sooner. They’re 64-bit IEEE 754 numbers, so they do have a maximum, about 1.8e308, and they lose integer precision past 2**53:

import math import sys print("largest float:", sys.float_info.max) print("times 10: ", sys.float_info.max * 10) # overflows to inf, no error print("math.inf > any int?", math.inf > 10 ** 1000) print("exact up to 2**53:", float(2**53) == 2**53, "| 2**53 + 1 survives?", float(2**53 + 1) == 2**53 + 1) try: float(10 ** 400) except OverflowError as err: print("OverflowError:", err)

Output:

largest float: 1.7976931348623157e+308 times 10: inf math.inf > any int? True exact up to 2**53: True | 2**53 + 1 survives? False OverflowError: int too large to convert to float

Multiplying past the max quietly gives inf, but converting a giant int to float raises OverflowError. I keep money and IDs as ints (or Decimal) so they stay exact, and you should too.

NumPy integers do overflow

NumPy uses fixed-size integers for speed, so int64 really does top out at 2**63 – 1. A scalar overflow warns and wraps around; inside an array it wraps silently:

import warnings import numpy as np warnings.simplefilter("always") info = np.iinfo(np.int64) print("int64 range:", info.min, "to", info.max) print("int32 max: ", np.iinfo(np.int32).max) x = np.int64(info.max) print("scalar + 1: ", x + np.int64(1)) # warns, then wraps around arr = np.array([info.max], dtype=np.int64) print("array + 1: ", arr + 1) # wraps silently in arrays print("as Python int:", int(info.max) + 1)

Output:

int64 range: -9223372036854775808 to 9223372036854775807 int32 max: 2147483647 C:\pyguides\numpy_int_max.py:10: RuntimeWarning: overflow encountered in scalar add print("scalar + 1: ", x + np.int64(1)) # warns, then wraps around scalar + 1: -9223372036854775808 array + 1: [-9223372036854775808] as Python int: 9223372036854775808
Command Prompt output of NumPy int64 limits, a RuntimeWarning on scalar overflow, and silent wraparound to a negative number in an arrayAdding 1 to the largest int64 gives the smallest negative number. Converting to a Python int avoids it.

Pandas columns use NumPy dtypes too, so very large IDs or totals can wrap. Check with np.iinfo() and use int64 or Python ints for big values. See NumPy matrix in Python for more NumPy basics.

Use math.inf instead of a max int value

When you need a starting value bigger than anything, like tracking a minimum, use math.inf (or -math.inf for a maximum). It compares correctly with every int and float:

import math prices = [19.99, 4.50, 102.00, 12.75] lowest = math.inf # better than a magic big number highest = -math.inf for p in prices: lowest = min(lowest, p) highest = max(highest, p) print(f"lowest ${lowest:.2f}, highest ${highest:.2f}") print(f"same with built-ins: ${min(prices):.2f} and ${max(prices):.2f}")

Output:

lowest $4.50, highest $102.00 same with built-ins: $4.50 and $102.00

For most real code, min() and max() do this for you. More on those in the max() function in Python.

Python number limits at a glance

TypeMaximumWhat happens past it
Python intNone (memory only)Keeps growing
sys.maxsize2**63 – 1 on 64-bitContainer sizes and indexes overflow
Python floatabout 1.8e308Becomes inf or raises OverflowError
NumPy int642**63 – 1Wraps around to negative
NumPy int322**31 – 1Wraps around to negative

Number guides that pair well with this one:

Frequently asked questions

What is the maximum value of int in Python?

There isn’t one in Python 3. Integers have unlimited precision and are limited only by available memory.

What is sys.maxsize in Python?

The largest value a Py_ssize_t can hold, 2**63 – 1 on 64-bit systems. It limits container sizes, not integer values.

Can Python integers overflow?

Plain Python ints can’t. NumPy and pandas fixed-size integers like int64 can, and they wrap around.

What’s the largest float in Python?

sys.float_info.max, about 1.8e308. Beyond that, results become inf.

How do I represent infinity in Python?

Use math.inf or float("inf"), and -math.inf for negative infinity.

Where is sys.maxsize documented?

See sys.maxsize in the Python docs.

Read Entire Article