Python Max int | Maximum value of int in Python

ItsMyCode |

In this tutorial, we will look at what’s Python Max int in different versions of Python.

Python 3 has unlimited precision that means there is no explicitly defined max limit. The amount of available address space is considered to be a practical maximum limit in Python 3.

Let’s take a look at different versions of Python and its max limit using examples.

Max int in Python 2

If you are using Python 2, you can find the maximum value of the integer using sys.maxint.

There is a max int limit in Python 2, and if you cross the max limit, it would automatically switch from int to long.

The max int limit in Python 2 is *9223372036854775807, * and anything above this number will be converted automatically into long.

Example: Python 2 Max int Limit

# Python 2 Max Int

import sys
print(sys.maxint)
print (type(sys.maxint))

Output

<type 'int'>
9223372036854775807

*Example: Python 2 Conversion from int to long *

# Python 2 Max int to long conversion

import sys

number= 9223372036854775807
print (type(number))
print(number)

new_number= number+1
print (type(new_number))
print(new_number)

Output

<type 'int'>
9223372036854775807
<type 'long'>
9223372036854775808

Max int in Python 3

** ** Python 3 does not have sys.maxint , and it does not have a maximum integer limit. However, we can use sys.maxsize to get the maximum value of the Py_ssize_t type in Python 2 and 3.

Example: Python 3 Max int Limit

# Python 3 Max Int

import sys
print(sys.maxsize)
print(type(sys.maxsize))

Output

9223372036854775807
<class 'int'>

In Python 2, once the max int limit is reached, the interpreter will convert into long, However in Python 3, both int and long are merged into one single datatype int, so there is no conversion.

The below example will show the Python interpreter word size, but technically, it does not have any max limit, and the amount of address available in the System is considered as the maximum limit.

So in our below example, you can see that the data type remains as an integer even after we increment the max integer number. We have also demonstrated that address is the max limit by storing a very long number and printing the type and value.

# Python 3 Max int 

import sys

number= 9223372036854775807
print (type(number))
print(number)

new_number= number+1
print (type(new_number))
print(new_number)

another_number=9999999999999999999999999999999
print (type(another_number))
print(another_number)

Output

<class 'int'>
9223372036854775807
<class 'int'>
9223372036854775808
<class 'int'>
9999999999999999999999999999999

28