Long integers:

There is no explicitly defined limit. The amount of available address space forms a practical limit.
(Taken from this site). See the docs on Numeric Types where you'll see that Long integers have unlimited precision. In Python 2, Integers will automatically switch to longs when they grow beyond their limit:

>>> import sys
>>> type(sys.maxsize)
<type 'int'>
>>> type(sys.maxsize+1)
<type 'long'>


for integers we have

maxint and maxsize:

The maximum value of an int can be found in Python 2.x with sys.maxint. It was removed in Python 3, but sys.maxsize can often be used instead. From the changelog:

The sys.maxint constant was removed, since there is no longer a limit to the value of integers. However, sys.maxsize can be used as an integer larger than any practical list or string index. It conforms to the implementation’s “natural” integer size and is typically the same as sys.maxint in previous releases on the same platform (assuming the same build options).

and, for anyone interested in the difference (Python 2.x):

sys.maxint The largest positive integer supported by Python’s regular integer type. This is at least 2**31-1. The largest negative integer is -maxint-1 — the asymmetry results from the use of 2’s complement binary arithmetic.

sys.maxsize The largest positive integer supported by the platform’s Py_ssize_t type, and thus the maximum size lists, strings, dicts, and many other containers can have.

and for completeness, here's the Python 3 version:

sys.maxsize An integer giving the maximum value a variable of type Py_ssize_t can take. It’s usually 2^31 - 1 on a 32-bit platform and 2^63 - 1 on a 64-bit platform.

floats:

There's float("inf") and float("-inf"). These can be compared to other numeric types:

>>> import sys
>>> float("inf") > sys.maxsize
True
Answer from keyser on Stack Overflow
🌐
Quora
quora.com › In-Python-range-x-cant-handle-long-integers-How-can-it-be-bypassed
In Python, range(x) can't handle long integers. How can it be bypassed? - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
Top answer
1 of 7
154

Long integers:

There is no explicitly defined limit. The amount of available address space forms a practical limit.
(Taken from this site). See the docs on Numeric Types where you'll see that Long integers have unlimited precision. In Python 2, Integers will automatically switch to longs when they grow beyond their limit:

>>> import sys
>>> type(sys.maxsize)
<type 'int'>
>>> type(sys.maxsize+1)
<type 'long'>


for integers we have

maxint and maxsize:

The maximum value of an int can be found in Python 2.x with sys.maxint. It was removed in Python 3, but sys.maxsize can often be used instead. From the changelog:

The sys.maxint constant was removed, since there is no longer a limit to the value of integers. However, sys.maxsize can be used as an integer larger than any practical list or string index. It conforms to the implementation’s “natural” integer size and is typically the same as sys.maxint in previous releases on the same platform (assuming the same build options).

and, for anyone interested in the difference (Python 2.x):

sys.maxint The largest positive integer supported by Python’s regular integer type. This is at least 2**31-1. The largest negative integer is -maxint-1 — the asymmetry results from the use of 2’s complement binary arithmetic.

sys.maxsize The largest positive integer supported by the platform’s Py_ssize_t type, and thus the maximum size lists, strings, dicts, and many other containers can have.

and for completeness, here's the Python 3 version:

sys.maxsize An integer giving the maximum value a variable of type Py_ssize_t can take. It’s usually 2^31 - 1 on a 32-bit platform and 2^63 - 1 on a 64-bit platform.

floats:

There's float("inf") and float("-inf"). These can be compared to other numeric types:

>>> import sys
>>> float("inf") > sys.maxsize
True
2 of 7
44

Python long can be arbitrarily large. If you need a value that's greater than any other value, you can use float('inf'), since Python has no trouble comparing numeric values of different types. Similarly, for a value lesser than any other value, you can use float('-inf').

🌐
Python
docs.python.org › 3 › c-api › long.html
Integer Objects — Python 3.14.7 documentation
CPython implementation detail: CPython keeps an array of integer objects for all integers between -5 and 256. When you create an int in that range you actually just get back a reference to the existing object.
Top answer
1 of 6
28

In old (2.x) versions of Python, xrange can only handle Python 2.x ints, which are bound by the native long integer size of your platform. Additionally, range allocates a list with all numbers beforehand on Python 2.x, and is therefore unsuitable for large arguments.

You can either switch to 3.x (recommended), or a platform where long int (in C) is 64 bit long, or use the following drop-in:

import itertools
range = lambda stop: iter(itertools.count().next, stop)

Equivalently, in a plain form:

def range(stop):
   i = 0
   while i < stop:
       yield i
       i += 1
2 of 6
6

This is what I would do:

def prime_factors(x):
    factors = []
    while x % 2 == 0:
        factors.append(2)
        x /= 2
    i = 3
    while i * i <= x:
        while x % i == 0:
            x /= i
            factors.append(i)
        i += 2
    if x > 1:
        factors.append(x)
    return factors

>>> prime_factors(600851475143)
[71, 839, 1471, 6857]

It's pretty fast and I think it's right. It's pretty simple to take the max of the factors found.


2017-11-08

Returning to this 5 years later, I would use yield and yield from plus faster counting over the prime range:

def prime_factors(x):
    def diver(x, i):
        j = 0
        while x % i == 0:
            x //= i
            j += 1
        return x, [i] * j
    for i in [2, 3]:
        x, vals = diver(x, i)
        yield from vals
    i = 5
    d = {5: 2, 1: 4}
    while i * i <= x:
        x, vals = diver(x, i)
        yield from vals
        i += d[i % 6]
    if x > 1:
        yield x

list(prime_factors(600851475143))

The dict {5: 2, 1: 4} uses the fact that you don't have to look at all odd numbers. Above 3, all numbers x % 6 == 3 are multiples of 3, so you need to look at only x % 6 == 1 and x % 6 == 5, and you can hop between these by alternately adding 2 and 4, starting from 5.

🌐
Python
peps.python.org › pep-0237
PEP 237 – Unifying Long Integers and Integers | peps.python.org
February 1, 2025 - If the long value doesn’t fit, it will still raise an OverflowError. For example: def fact(n): if n <= 1: return 1 return n*fact(n-1) A = "ABCDEFGHIJKLMNOPQ" n = input("Gimme an int: ") print A[fact(n)] For n >= 13, this currently raises OverflowError (unless the user enters a trailing L as part of their input), even though the calculated index would always be in range(17).
🌐
Fangohr
fangohr.github.io › blog › performance-of-pythons-long-data-type.html
Performance of Python's long data type — Computational Science and Data Science
This is the range of numbers up to 1,000,000,000 which is below sys.maxint as shown above, and thus the operations are carried out in the hardware. For 10 and more digits, the numbers $\ge 10^{10}$ are emulated in software, and thus execution time increases significantly.
🌐
Python.org
discuss.python.org › ideas
Allow `range(start, None, step)` for an endless range - Ideas - Discussions on Python.org
January 15, 2021 - Currently, to iterate over finite arithmetic sequences of integers, range is used, as in: for i in range(10): print(i) For an infinite arithmetic sequence, there are a few approaches. One can replace the ‘10’ with a long sequence of nines, write a generator function or just switch to a ...
🌐
Medium
mike-vincent.medium.com › quarks-outlines-python-long-integers-3d41b1f0681f
Quark’s Outlines: Python Long Integers | by Mike Vincent | Medium
August 4, 2025 - Solution: Python long integers avoid this problem by design. Python lets you grow values without hidden limits or failure. amount = 1_000_000_000 for i in range(10): amount = amount * 10 print(amount) # Output: 10000000000000000000000
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › ints
int — Python Reference (The Right Way) 0.1 documentation
(The range may be larger on machines with a larger natural word size, but not smaller.) When the result of an operation would fall outside this range, the result is normally returned as a long integer (in some cases, the exception OverflowError is raised instead).
Find elsewhere
🌐
DEV Community
dev.to › mike-vincent › quarks-outlines-python-long-integers-36nl
Quark’s Outlines: Python Long Integers - DEV Community
August 4, 2025 - Problem: You want to prevent silent failure from number overflow. Solution: Python long integers avoid this problem by design. Python lets you grow values without hidden limits or failure. amount = 1_000_000_000 for i in range(10): amount = amount * 10 print(amount) # Output: 10000000000000000000000
🌐
Codementor
codementor.io › community › how python implements super long integers?
How python implements super long integers? | Codementor
January 10, 2020 - Instead of storing just one decimal digit in each item of the array ob_digit, python converts the number from base 10 to base 2³⁰ and calls each of element as digit which ranges from 0 to 2³⁰ - 1. In the hexadecimal number system, the base is 16 ~ 2⁴ this means each "digit" of a hexadecimal number ranges from 0 to 15 of the decimal system.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Integer (int) Has No Max Limit in Python 3 | note.nkmk.me
January 27, 2024 - The int type in Python 3, equivalent to the long type in Python 2, has no max or min limit.
🌐
GeeksforGeeks
geeksforgeeks.org › what-is-the-maximum-possible-value-of-an-integer-in-python
What is the maximum possible value of an integer in Python ? - GeeksforGeeks
July 28, 2022 - Thus we never need any special arrangement for storing large numbers (Imagine doing above arithmetic in C/C++). As a side note, in Python 3, there is only one type "int" for all type of integers. In Python 2.7. there are two separate types "int" (which is 32 bit) and "long int" that is same as "int" of Python 3.x, i.e., can store arbitrarily large numbers.
🌐
DataCamp
datacamp.com › tutorial › everything-you-need-to-know-about-pythons-maximum-integer-value
What You Need to Know About Python's Maximum Integer Value | DataCamp
May 6, 2024 - The long data type had no upper limit except as constrained by available memory on the system. In Python 3, however, because the int data type supports arbitrary precision, Python doesn't need to convert the int type into a long when working ...
🌐
Real Python
realpython.com › python-range
Python range(): Represent Numerical Ranges – Real Python
September 9, 2025 - In Python, the range() function generates a sequence of numbers, often used in loops for iteration. By default, it creates numbers starting from 0 up to but not including a specified stop value. You can also reverse the sequence with reversed().
🌐
AlgoMaster
algomaster.io › home › python › len & range
len & range | Python | AlgoMaster.io
June 6, 2026 - It works on sequences (lists, tuples, strings, bytes, ranges), mappings (dicts, sets, frozensets), and any user-defined class that implements __len__. For a dict, len(cart) counts the keys (which is the same as counting entries). For a string, it counts characters, not bytes, because Python 3 strings are Unicode code points.