Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How does Python handle very large integers?

Python
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 25, 2026
6 min read

Python handles very large integers natively: the built-in int type has arbitrary precision, so it grows as needed and is limited only by available memory. There is no overflow and no separate "long" type, so 2**1000 or factorial(500) simply works. These Python big integers are stored as arrays of 30-bit digits, which makes them slower than machine integers but always exact.

This article covers Python 3.13 and 3.14. It explains how CPython stores large values, what they cost in memory and time, and the one safety limit you can hit when converting huge integers to text.

Table of Contents

  • Arbitrary precision in practice
  • How CPython stores big integers
  • Speed and algorithmic cost
    • Modular arithmetic without huge intermediates
  • The integer string conversion limit
  • When Python big integers are not enough
  • Frequently Asked Questions
    • Is there a maximum integer in Python?
    • Why does int(str) fail on a long string of digits?
    • Are big integers slower than normal integers?
    • How do I do exact large-number math with NumPy?
  • Summary

Arbitrary precision in practice

In Python 3 every integer is an int, and an int never overflows. In Python 2, int and long were separate types, but they were merged in Python 3.

python
text

Arithmetic on these values is always exact. Compare that with fixed-width integers in C, Java or Go, where int64 wraps around once it passes 9,223,372,036,854,775,807. Python never wraps.

For readability you can write large literals with underscores, and you can use bit_length() to see how large a value is in bits:

python

How CPython stores big integers

CPython stores an int as a sign plus a sequence of "digits", each holding 30 bits on 64-bit builds (the digit is a 32-bit unsigned integer with 30 bits used). You can inspect this with sys.int_info:

python
text

Memory grows by 4 bytes for every extra 30 bits on top of a fixed object header, so a 1,000-bit number takes roughly 160 bytes. The exact sizes can vary by build and Python version, so treat sys.getsizeof as the source of truth on your machine.

Two implementation details are worth knowing:

  • CPython caches small integers from -5 to 256, so they are shared objects. This is an implementation detail; always compare numbers with ==, never is.
  • Values that fit in one digit use fast paths. Anything larger uses the general multi-digit algorithms.

Speed and algorithmic cost

Big-integer arithmetic costs more as numbers grow. Addition and subtraction are linear in the number of digits. Multiplication uses the schoolbook method for small sizes and Karatsuba multiplication for large ones, which is faster than quadratic. Division and conversion to a decimal string are quadratic or worse in the general case.

OperationRough cost for n digits
Addition, subtractionLinear
MultiplicationBetween linear and quadratic (Karatsuba for large values)
Division, moduloQuadratic
str(n) and int(s) in base 10Quadratic in CPython
Bit shifts, &, |, ^Linear

For everyday numbers the overhead is negligible. It matters in tight loops, for example computing very large Fibonacci or factorial values, where the time is dominated by the size of the operands.

Modular arithmetic without huge intermediates

Cryptography-style code often needs a ** b % m for huge b. Do not compute the power first. The three-argument pow uses fast modular exponentiation and keeps every intermediate value small:

python
text

The first call finishes instantly. Writing 7 ** (10**18) % 1_000_000_007 would try to build a number with hundreds of quadrillions of digits and never finish.

The integer string conversion limit

Since Python 3.11 (and backported to supported 3.7 to 3.10 security releases) CPython limits integer-to-string and string-to-integer conversions in bases that are not powers of two. The default is 4,300 digits. It exists because the quadratic conversion cost made a short malicious input a denial-of-service risk.

python
text

The limit applies to int(s), str(n), repr(n) and f-strings in base 10. It does not apply to hexadecimal, octal or binary, so hex(n) always works. You can change it in three ways:

  1. Call sys.set_int_max_str_digits(0) in your program (0 disables the limit).
  2. Set the environment variable PYTHONINTMAXSTRDIGITS.
  3. Start Python with -X int_max_str_digits=N.

Raise the limit only for trusted input. If you parse numbers from users, keep the default and handle the ValueError.

When Python big integers are not enough

Python integers are exact but slow compared with fixed-width ones. Use other tools in these cases:

  • NumPy arrays use fixed-width types such as int64 and do overflow silently. Use dtype=object for exact values, at the cost of speed.
  • For very large computations (millions of digits), the gmpy2 package wraps the GMP library and is much faster than the built-in type.
  • For fractional values, an int is the wrong type; see Fraction and Decimal in the standard library.

Frequently Asked Questions

Is there a maximum integer in Python?

There is no maximum for int. The practical limit is available memory. sys.maxsize is only the largest value a C Py_ssize_t can hold, which is used for container sizes and indexes.

Why does int(str) fail on a long string of digits?

You are hitting the 4,300-digit conversion limit added in Python 3.11. Use sys.set_int_max_str_digits() for trusted data, or split and process the input differently.

Are big integers slower than normal integers?

Yes, once a value exceeds one 30-bit digit and especially for very large values. For typical program values the difference is not noticeable.

How do I do exact large-number math with NumPy?

Create the array with dtype=object so each element is a Python int. Fixed-width dtypes overflow without raising an error.

Summary

Python's int has arbitrary precision, stored as 30-bit digits, so large values are exact and never overflow. Costs grow with size, multiplication uses Karatsuba, and base-10 conversion is limited to 4,300 digits by default since Python 3.11. Use pow(a, b, m) for modular exponentiation and reach for gmpy2 only when profiling shows big-integer arithmetic is your bottleneck.

Share

Similar posts

  • How do you format numbers with f-strings in Python?
  • How do you use f-strings in Python?
  • What are escape sequences in Python strings?
  • Why are strings immutable in Python?
  • How do you check if a string is a number in Python?
  • How do you count occurrences of a substring in Python?
  • How do you check if a string contains a substring in Python?
  • How do you change the case of a string in Python?