Integer (int) has no max limit in Python 3
In Python 3, the int
type has no max limit. You can handle values as large as the available memory allows.
Python 2 has two integer types, int
and long
, whereas Python 3 has only int
. The int
in Python 3 is equivalent to the long
in Python 2, with no max limit.
For information on the max and min float
values, refer to the following article.
NumPy uses fixed-bit data types, such as int32
(32-bit integer) and int64
(64-bit integer).
int
and long
in Python 2
Python 2 has two integer types: int
and long
.
In Python 2, the max int
value can be obtained using sys.maxint
. The min int
value, which is the largest negative number, is -sys.maxint - 1
.
sys.maxint
typically has a value of 2**31 - 1
in a 32-bit environment and 2**63 - 1
in a 64-bit environment.
The long
type has no max or min limit.
int
has no max limit in Python 3
The int
type in Python 3, equivalent to the long
type in Python 2, has no max or min limit.
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 assys.maxint
in previous releases on the same platform (assuming the same build options). What’s New In Python 3.0 — Python 3.12.1 documentation
In Python 3, sys.maxint
has been removed, and sys.maxsize
has been added.
sys.maxsize
is 2**31 - 1
in a 32-bit environment and 2**63 - 1
in a 64-bit environment, similar to sys.maxint
in Python 2.
import sys
print(sys.maxsize)
# 9223372036854775807
print(type(sys.maxsize))
# <class 'int'>
print(sys.maxsize == 2**63 - 1)
# True
When converted to binary and hexadecimal strings using bin()
and hex()
, sys.maxsize
is expressed as follows.
print(bin(sys.maxsize))
# 0b111111111111111111111111111111111111111111111111111111111111111
print(hex(sys.maxsize))
# 0x7fffffffffffffff
sys.maxsize
is not the max int
value; you can handle larger values as long as there is sufficient available memory.
i = 10**100
print(i)
# 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
print(i > sys.maxsize)
# True
Comparison with float
sys.float_info.max
provides the max float
value.
print(sys.float_info.max)
# 1.7976931348623157e+308
With no max limit, int
can handle values exceeding the max float
value.
i_e309 = 10**309
print(type(i_e309))
# <class 'int'>
print(i_e309 > sys.float_info.max)
# True
Note that float
represents infinity as inf
, which is considered larger than any other numerical value.
print(float('inf'))
# inf
print(float('inf') > sys.float_info.max)
# True
print(float('inf') > i_e309)
# True
Converting inf
to int
results in an OverflowError
.
# int(float('inf'))
# OverflowError: cannot convert float infinity to integer
For details about infinity (inf
), refer to the following article.
Integer string conversion length limitation
In versions released after September 7, 2022 (Python 3.11, 3.10.7, 3.9.14, 3.8.14, 3.7.14, and later), integer-string conversion is limited to 4300 digits by default.
This limitation aims to prevent DoS attacks that exploit the O(n^2)
complexity of integer-string conversions.
This limit applies only to conversion; int
can still represent values larger than 4300 digits, and other operations remain unaffected.
Be aware that functions that internally convert integers to strings, such as print()
or repr()
, are also subject to this limit.
i = 10**10000
# print(i)
# ValueError: Exceeds the limit (4300 digits) for integer string conversion; use sys.set_int_max_str_digits() to increase the limit
For details on how to set the digit limit, refer to the following article.