The range of integer in Python???

Tim Peters tim.one at home.com
Sat Jan 13 22:05:12 EST 2001


[cnc]
> What is the range of integer numerical type in Python?

There are two integer types in Python.  "longs" are unbounded, and grow as
large as needed until you run out of memory.  "ints" are exactly the same as
the "signed long" type under the C used to compile Python.  That's 32 bits
on most platforms these days, but 64 bits is less rare by the decade <wink>.

> I read some introduction book of Python, they say it ranges
> from (-maxint, maxint).  However, I guess, it should be
> (-maxint-1, maxint).

maxint is an attribute of Python's sys module, so change that to

    from and including -sys.maxint-1,
    up to and including sys.maxint

and that's the range of Python's bounded integers on all platforms today.

>>> import sys
>>> sys.maxint
2147483647
>>> sys.maxint+1
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
OverflowError: integer addition
>>> -sys.maxint-1
-2147483648
>>> -sys.maxint-2
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
OverflowError: integer subtraction
>>>





More information about the Python-list mailing list