. Advertisement .
. Advertisement .
There are some scenarios you may want to know the max value of integers in Python.
Some programmers are curious about the underlying mechanism of this programming language, while others need this information to avoid overflow issues after checking its data type. In either case, here is the answer you need.
Max Value Of Integers In Python 2
There are two distinct integer types in Python 2.x: plain and long integers.
The reference implementation of Python (CPython) uses the long data type in C to implement plain integers (more commonly known as just integers or int).
This design means the range of int values in Python depends on the operating system. To be more specific, 32-bit systems use 4 bytes (or 32 bits) to present long values in C, while 64-bit systems use 8 bytes (or 64 bits).
Remember that int values in Python are signed (they can be positive or negative). This means their maximum positive value is 216-1 and 232-1 in 32-bit and 64-bit platforms. Likewise, their minimum values are -216 and -232, respectively.
On the other hand, long integers in Python have unlimited precision by default. When plain integers (int) values overflow, Python will convert them to long ints automatically.
In Python 2.x, you can use the maxint constant in the sys module to find out the maximum integer value in your system.
>>> import sys >>> sys.maxint # 64-bit systems 9223372036854775807
Per Python 2’s official documentation, the sys.maxint value indicates your Python version’s largest positive regular integer. You can see how Python 2.x automatically stores integer variables and constants in the correct type (int or long) based on their value.
>>> import sys >>> a = sys.maxint >>> type(a) <type 'int'> >>> a += 1 >>> type(a) <type 'long'>
The variable a is first stored as an int value because it doesn’t go beyond the range of this number type. However, Python converts it into a long variable when it becomes larger than int’s maximum possible value.
You can also use it to calculate the smallest negative integer your Python system supports.
>>> import sys >>> -sys.maxint - 1 # 64-bit systems -9223372036854775808
Max Value Of Integers In Python 3
The upgrade to the 3.x version brings several substantial fundamental changes to Python. For instance, the long integer type has been removed. Every integer value in Python 3.x is now represented only by one built-in type: int.
Despite its name, the long data type in Python 3 behaves like Python 2’s long. It means there is no constraint on its limitation. You can now store a value as big as you wish in an int value.
The maxint constant has also been removed from the sys module since there is no longer use for it.
>>> import sys >>> sys.maxint
Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> sys.maxint AttributeError: module 'sys' has no attribute 'maxint'
The max value of integers in Python depends on the operating system and Python version you are using.
There is no limitation to how big integer values can be in Python 3. However, this applies only to long values in Python 2. The maximum value of int values depends on whether you are running a 32-bit or 64-bit system.
Leave a comment