patternpythonCritical
Maximum and Minimum values for ints
Viewed 0 times
intsvaluesmaximumandforminimum
Problem
How do I represent minimum and maximum values for integers in Python? In Java, we have
See also: What is the maximum float in Python?.
Integer.MIN_VALUE and Integer.MAX_VALUE.See also: What is the maximum float in Python?.
Solution
Python 3
In Python 3, this question doesn't apply. The plain
However, you might actually be looking for information about the current interpreter's word size, which will be the same as the machine's word size in most cases. That information is still available in Python 3 as
Generally, the maximum value representable by an unsigned word will be
Python 2
In Python 2, the maximum value for plain
You can calculate the minimum value with
Python seamlessly switches from plain to long integers once you exceed this value. So most of the time, you won't need to know it.
In Python 3, this question doesn't apply. The plain
int type is unbounded.However, you might actually be looking for information about the current interpreter's word size, which will be the same as the machine's word size in most cases. That information is still available in Python 3 as
sys.maxsize, which is the maximum value representable by a signed word. Equivalently, it's the size of the largest possible list or in-memory sequence.Generally, the maximum value representable by an unsigned word will be
sys.maxsize 2 + 1, and the number of bits in a word will be math.log2(sys.maxsize 2 + 2). See this answer for more information.Python 2
In Python 2, the maximum value for plain
int values is available as sys.maxint:>>> sys.maxint # on my system, 2**63-1
9223372036854775807You can calculate the minimum value with
-sys.maxint - 1 as shown in the docs.Python seamlessly switches from plain to long integers once you exceed this value. So most of the time, you won't need to know it.
Code Snippets
>>> sys.maxint # on my system, 2**63-1
9223372036854775807Context
Stack Overflow Q#7604966, score: 1345
Revisions (0)
No revisions yet.