DEV Community

Srinivas Ramakrishna for ItsMyCode

Posted on • Originally published at itsmycode.com on

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 Comment 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))

Enter fullscreen mode Exit fullscreen mode

Output

<type 'int'>
9223372036854775807
Enter fullscreen mode Exit fullscreen mode

*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)
Enter fullscreen mode Exit fullscreen mode

Output

<type 'int'>
9223372036854775807
<type 'long'>
9223372036854775808
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

Output

9223372036854775807
<class 'int'>
Enter fullscreen mode Exit fullscreen mode

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)

Enter fullscreen mode Exit fullscreen mode

Output

<class 'int'>
9223372036854775807
<class 'int'>
9223372036854775808
<class 'int'>
9999999999999999999999999999999
Enter fullscreen mode Exit fullscreen mode

The post Python Max int | Maximum value of int in Python appeared first on ItsMyCode.

Top comments (1)

Collapse
 
rohit254 profile image
Rohit Kumar • Edited

I didn't know there was something called maxsize in Python to get the maximum number of an integer.

I have a basic Python blog in which I want reviews can anyone review it and provide their insights? Here is the link: -
[surushatutorials.blogspot.com/2024...]