As 70% of the folks who start learning Python come from C, C++, or Java background. So, while learning the Python programming language many times, a question arises in our mind: what’s the Python max int or the maximum value of integer data type in Python? If you have the same query in your mind, don’t worry; you are in the right place, and we will clear all of your doubts in this article.
When it comes to a max int value in Python, this query doesn’t apply to the Python programming language. As we all know, python is a cool and modern programming language. So, while talking about Python, we can say the int data type in Python is unbounded by any limits and restrictions.
Let’s see practical examples to prove that there are unlimited precision and no explicitly defined limits for the maximum value of an integer in Python.
Finding Maximum Possible Value of an Integer [Python Max Int] in Python 3
To check if there is any restraint or limit in Python max int value. We will take an example and try to print an integer value that is as large as possible. So, let’s directly jump into the example.
Example to check if there is any Maximum integer value [Python Max Int] in python 3
n = 99999999999999999999999999999999999999999999999999999999
n = n + 1
print (n)
print(type(n))
Output:
100000000000000000000000000000000000000000000000000000000
<class 'int'>
From the above example, it’s now clear that there is no limit for max int in Python. The number of bits does not restrict the value of an integer, but it depends on the size of the available memory.
Finding Maximum Possible Value of an Integer [Python Max Int] in Python 2
n = 999
print(type(n))
n = 99999999999999999999999999999999999999999999999999999999
print(type(n))
Output:
<type 'int'>
<type 'long'>
So, from the above example, it’s now clear that there is some maximum value of integer (Python Max int) in Python 2. To find out the maximum value of an integer in Python 2, we will use a constant sys.maxint.
Let’s see an example of sys.maxint constant in python 2.
Example to find maximum value [Python Max Int] using sys.maxint in python 2.7
import sys
print sys.maxint
print type(sys.maxint)
print type(sys.maxint + 1)
Output:
9223372036854775807
<type 'int'>
<type 'long'>
Explanation:
From the above example, we can say that there is some limit in the integer size when we talk about Python 2.
But we don’t need to bother about the size of integer or any other data type. As Python seamlessly switches from plain integer to long integers once you exceed this value.
Note:
As a side note, in Python 3, there’s only 1 type”int” for all sorts of integers. In Python 2.7. There are two distinct types”int” (which is 32-bit) and”long int” which are the same as”int” of Python 3.x, i.e., can store arbitrarily large amounts.
Does sys.maxint Still Works with Python 3?
As we already discussed in the above sections, there is no limit for integers in python 3. So, the sys.maxint constant was eliminated as there is no longer a limit to the value of integers. However, sys.maxsize may be utilized as an integer more significant than any sensible list or series index. It conforms to the execution’s”natural” integer size and is typically the same as sys.maxint in previous releases on the same stage (assuming the same build options).
Checking by example if sys.maxint works in python 3
import sys
print(type(sys.maxint))
Output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'sys' has no attribute 'maxint'
So, from the above example we have proved that there is no sys.maxint available in python 3. However we can use sys.maxsize in place of sys.maxint.
Learn More: http://docs.python.org/3.1/whatsnew/3.0.html#integers
Using sys.maxsize to Find Python Max Int in Python 3
As from the above, we can now conclude that in Python 3, there is no constant named sys.maxint. So, instead of this, we are using another constant sys.maxsize which works quite similarly to sys.maxint.
Let’s directly jump into the example and try to find the maximum value of the integer using sys.maxsize.
Example of seeking the maximum value of an integer [Python Max Int] in Python 3 [sys.maxsize]
import sys
print(type(sys.maxsize))
print(type (sys.maxsize + 1))
Output:
<class 'int'>
<class 'int'>
Explanation:
In the above example, we can clearly see that if we are using Python 3, then there is no maximum value for the integer. Even after incrementing the sys.maxsize constant by 1, the data type remains of type integer. So, now we can say that in Python 3 int and long are actually merged and the value has no such importance.
Must Read
- Introduction to Python Super With Examples
- Python Help Function
- Why is Python sys.exit better than other exit functions?
- Python Bitstring: Classes and Other Examples | Module
Conclusion
If you make it till the end, I am pretty sure you now be able to understand the concept behind the maximum value in Python or Python max int. I think you might also want to know Ways in Python to Sort the List of Lists. If yes, there is an awesome tutorial available in our library of tutorials do check it out.
If you have any doubts or questions do let me know in the comment section below. I will try to help you as soon as possible.
Happy Pythoning!
The integer limit is equal to 2 to the power of the python RAM limit in bytes, if you want to find the python RAM limit check the RAM limit per program
I disagree with your statement. There is a clear mention that the integer limit is nondependent on the memory limit. As python3 operates on long int, The long int is not restricted by the number of bits and can expand to the limit of the available memory. Source: Texas Univ Lecture.