How do I represent minimum and maximum values for integers in Python? In Java, we have Integer.MIN_VALUE
and Integer.MAX_VALUE
.
See also: What is the maximum float in Python?.
How do I represent minimum and maximum values for integers in Python? In Java, we have Integer.MIN_VALUE
and Integer.MAX_VALUE
.
See also: What is the maximum float in Python?.
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.
In Python 2, the maximum value for plain int
values is available as sys.maxint
:
>>> sys.maxint # on my system, 2**63-1
9223372036854775807
You 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.
2^63 - 1
, so you've got a 64-bit int. In general, an n-bit integer has values ranging from -2^(n-1)
to 2^(n-1) - 1
.
Sep 30, 2011 at 1:18
2^31 - 1
, even though Python will jump to 64-bit seamlessly with the long
datatype.
Feb 26, 2014 at 16:19
sys.maxsize
instead, as suggested by @Akash Rana. It is present also in Python 2, as sys
docs say. This will make the code more compatible with both Python versions.
2to3
is a fine quick-and-dirty heuristic that won't break anything most of the time -- but the difference between these two values matters. The best practice is to use the value you actually mean to use. If you truly need sys.maxint
in Python 2, you won't need it anymore in Python 3, and it should really be removed entirely, not changed to sys.maxsize
.
If you just need a number that's bigger than all others, you can use
float('inf')
in similar fashion, a number smaller than all others:
float('-inf')
This works in both python 2 and 3.
x > x
is usually False
, and infinity should be no exception. (float('NaN)
, on the other hand...)
Sep 6, 2017 at 2:03
int
cauze cannot convert infinite float to int
...but works for most cases
The sys.maxint
constant has been removed from Python 3.0 onward, instead use sys.maxsize
.
Integers
- PEP 237: Essentially,
long
renamed toint
. That is, there is only one built-in integral type, namedint
; but it behaves mostly like the oldlong
type....
- The
sys.maxint
constant was removed, since there is no longer a limit to the value of integers. However,sys.maxsize
can be used as an integer larger than any practical list or string index. It conforms to the implementation’s “natural” integer size and is typically the same assys.maxint
in previous releases on the same platform (assuming the same build options).
help(sys)
: maxsize -- the largest supported length of containers. This should be the accepted answer.
Dec 29, 2019 at 21:53
For Python 3, there is no maximum or minimum value for the int
type.
You might be interested in sys.maxsize
instead. According to the documentation:
sys.maxsize
An integer giving the maximum value a variable of type
Py_ssize_t
can take. It’s usually2**31 - 1
on a 32-bit platform and2**63 - 1
on a 64-bit platform.
import sys
max_size = sys.maxsize
min_size = -sys.maxsize - 1
sys.maxint
doesn't exist in python 3 (tl;dr: "sys.maxint
constant was removed (in python3), since there is no longer a limit to the value of integers. However, sys.maxsize
can be used as an integer larger than any practical list or string index." )
sys.maxsize ** 2
would not return a valid and correct value, yet it does. You should delete this answer.
Nov 12, 2019 at 17:12
In Python 2, integers will automatically switch from a fixed-size int
representation into a variable width long
representation once you pass the value sys.maxint
, which is either 231 - 1 or 263 - 1 depending on your platform. Notice the L
that gets appended here:
>>> 9223372036854775807
9223372036854775807
>>> 9223372036854775808
9223372036854775808L
From the Python 2.7 manual:
Numbers are created by numeric literals or as the result of built-in functions and operators. Unadorned integer literals (including binary, hex, and octal numbers) yield plain integers unless the value they denote is too large to be represented as a plain integer, in which case they yield a long integer. Integer literals with an
'L'
or'l'
suffix yield long integers ('L'
is preferred because1l
looks too much like eleven!).
Python tries very hard to pretend its integers are mathematical integers and are unbounded. It can, for instance, calculate a googol with ease:
>>> 10**100
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000L
long
isn't like Java's long
- it's rather closer to BigInteger
.
Sep 30, 2011 at 1:20
L
suffix, and it's just int
, not long
, no matter how large the number is.
type(...)
instead on relying on an L
at the end which an have ambiguous meaning?
You may use 'inf' like this:
import math
bool_true = 0 < math.inf
bool_false = 0 < -math.inf
math.inf
and -math.inf
cannot be converted into int, e.g. this will fail: a = math.inf; b = int(a)
. It will throw OverflowError: cannot convert float infinity to integer
. You can use it, but be aware it is outside the number range of int
. So you can't use it as a sentinel value.
If you want the max for array or list indices (equivalent to size_t
in C/C++), you can use numpy:
np.iinfo(np.intp).max
This is same as sys.maxsize
however advantage is that you don't need import sys just for this.
If you want max for native int on the machine:
np.iinfo(np.intc).max
You can look at other available types in doc.
For floats you can also use sys.float_info.max
.
sys.maxsize
is not the actually the maximum integer value which is supported. You can double maxsize and multiply it by itself and it stays a valid and correct value.
However, if you try sys.maxsize ** sys.maxsize
, it will hang your machine for a significant amount of time. As many have pointed out, the byte and bit size does not seem to be relevant because it practically doesn't exist. I guess python just happily expands it's integers when it needs more memory space. So in general there is no limit.
Now, if you're talking about packing or storing integers in a safe way where they can later be retrieved with integrity then of course that is relevant. I'm really not sure about packing but I know python's pickle
module handles those things well. String representations obviously have no practical limit.
So really, the bottom line is: what is your applications limit? What does it require for numeric data? Use that limit instead of python's fairly nonexistent integer limit.
I rely heavily on commands like this.
python -c 'import sys; print(sys.maxsize)'
Max int returned: 9223372036854775807
For more references for 'sys' you should access
On CPython 3.11 on a 64-bit system, the maximum and minimal integers are
2 ** ((2 ** 63 - 1) * 30) - 1
-(2 ** ( 2 ** 63 * 30) - 1)
You will need 40 exabytes of memory to create one, which would cost $70 billion at today's (July 2023) prices of $57 per 32GB on NewEgg, so in practice Python's maximum integer is limited by how much memory you have in your computer.
CPython 3.11 stores integers like this (I simplified the actual code by taking out all the macros):
struct PyLongObject {
Py_ssize_t ob_refcnt; /* Metadata for garbage collection */
PyTypeObject* ob_type; /* Metadata for type() */
Py_ssize_t ob_size; /* Number of items in ob_digit */
uint32_t ob_digit[1]; /* Array of 32-bit integers */
};
So on a 64-bit system, Python integers are implemented as an array of 32-bit integers storing the absolute value of the integer (but 2 of the bits of each integer aren't used) and a 64-bit signed two's complement integer stores the length of that array as well as the sign of the Python integer, so a negative integer has a negative "size".
As other answers point out, unlike float
(which has the maximum value of sys.float_info.max
), int
type is unbounded in Python; it is really bounded by the memory limit of your machine. That said, there are some limits related to integers.
For example, since Python 3.10.7, converting str
to int
or printing int
is limited to 4300 digits, a consequence of which is writing a very long integer to a file is limited by default (which can be turned off by running sys.set_int_max_str_digits(0)
). You can get information about the internal representation of integers in Python using sys.int_info
.
Also since float
is bounded, you can't perform calculations where the output would become a float that is outside float bound (e.g. 2**1025/2
raises an OverflowError
).
Also, int
in popular data analysis libraries such as numpy and pandas etc. are bound by their type, e.g. numpy.uint64
can hold the largest positive integer in numpy, which can be checked by np.iinfo('uint64')
. This means we can't perform vectorized computations involving too large integers, e.g. we can't take log2 of 2**64 (in fact, np.log2(2**64)
raises a TypeError).
sys.maxsize
is the size of the largest possible list. You can check that by using range()
.
len(range(sys.maxsize)) == sys.maxsize # 9223372036854775807
len(range(sys.maxsize+1)) # OverflowError
code given below will help you. for maximum value you can use sys.maxsize and for minimum you can negate same value and use it.
import sys
ni=sys.maxsize
print(ni)
int
type is basically the same as thelong
type in Python 2, so the idea of a maximum or minimumint
disappears completely. It's basically irrelevant even on Python 2.sys.maxint
since it's only the maximum for theint
type on Python 2, which Python will silently promote to along
.float('inf')
orfloat('-inf')
can be quite helpful.Literal
in type hints. So you can't say that a list can containUnion[int, Literal[-inf]]
even though that might be exactly what might be needed for a given application :/