[Tutor] range of int

Magnus Lyckå magnus@thinkware.se
Thu Jul 3 10:15:02 2003


At 19:22 2003-07-03 +0530, Jimmy verma wrote:
>Can some please tell me the range of interger variables in python.  Like 
>in C we have -32768-32767.

A normal Python int is 32 bits on a 32 bit computer.
You can find the limits in the sys module.

 >>> import sys
 >>> print sys.maxint
2147483647

On a 64 bit system such as an Alpha, Itanium or Ultra Sparc, this
would probably be 4611686018427387903, but it depends on the
underlying C library.

The largest possible int is sys.maxint, and the smallest
possible is -1-sys.maxint.

But note that modern Python versions will silently convert
to long integers when calculation results no longer fit in
an int. Thus int + int will return int or long depending on
values. From version 2.3 the builtin int type/factory function
will return longs if fed with a value that doesn't fit in the
int type. With 2.2 you still get an overflow.

 >>> sys.maxint
2147483647
 >>> sys.maxint+1
2147483648L
 >>> int(sys.maxint)
2147483647
 >>> int(sys.maxint+1)
Traceback (most recent call last):
   File "<interactive input>", line 1, in ?
OverflowError: long int too large to convert to int

In Python 2.3, int(sys.maxint+1) will return a long. This
is a part of an ongoing merge of the long and int types.
Eventually, Python will only have one integer type, and it's
up to the implementation, not to the language standard to
handle small integers in an effective way.

Python's long integers can be as large as you like, as long
as your computer doesn't run out of memory...


--
Magnus Lycka (It's really Lyck&aring;), magnus@thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The Agile Programming Language