Section 5.2. Integers


5.2. Integers

Python has several types of integers. There is the Boolean type with two possible values. There are the regular or plain integers: generic vanilla integers recognized on most systems today. Python also has a long integer size; however, these far exceed the size provided by C longs . We will take a look at these types of integers, followed by a description of operators and built-in functions applicable only to Python integer types.

5.2.1. Boolean

The Boolean type was introduced in Python 2.3. Objects of this type have two possible values, Boolean TRue and False. We will explore Boolean objects toward the end of this chapter in Section 5.7.1.

5.2.2. Standard (Regular or Plain) Integers

Python's "plain" integers are the universal numeric type. Most machines (32-bit) running Python will provide a range of -231 to 231-1, that is -2, 147,483,648 to 2,147,483,647. If Python is compiled on a 64-bit system with a 64-bit compiler, then the integers for that system will be 64-bit. Here are some examples of Python integers:

0101    84      -237    0x80    017     -680    -0X92


Python integers are implemented as (signed) longs in C. Integers are normally represented in base 10 decimal format, but they can also be specified in base 8 or base 16 representation. Octal values have a "0" prefix, and hexadecimal values have either "0x" or "0X" prefixes.

5.2.3. Long Integers

The first thing we need to say about Python long integers (or longs for short) is not to get them confused with longs in C or other compiled languagesthese values are typically restricted to 32- or 64-bit sizes, whereas Python longs are limited only by the amount of (virtual) memory in your machine. In other words, they can be very L-O-N-G longs.

Longs are a superset of integers and are useful when your application requires integers that exceed the range of plain integers, meaning less than -231 or greater than 231-1. Use of longs is denoted by the letter "L", uppercase (L) or lowercase (l), appended to the integer's numeric value. Values can be expressed in decimal, octal, or hexadecimal. The following are examples of longs:

16384L     -0x4E8L 017L  -2147483648l   052144364L 299792458l 0xDECADEDEADBEEFBADFEEDDEAL    -5432101234L


Core Style: Use uppercase "L" with long integers

Although Python supports a case-insensitive "L" to denote longs, we recommend that you use only the uppercase "L" to avoid confusion with the number one (1). Python will display only longs with a capital "L ." As integers and longs are slowly being unified, you will only see the "L" with evaluatable string representations (repr()) of longs. Printable string representations (str()) will not have the "L ."

>>> aLong = 999999999l >>> aLong 999999999L >>> print aLong 999999999



5.2.4. Unification of Integers and Long Integers

Both integer types are in the process of being unified into a single integer type. Prior to Python 2.2, plain integer operations resulted in overflow (i.e., greater than the 232 range of numbers described above), but in 2.2 or after, there are no longer such errors.

Python 2.1

>>> 9999 ** 8 Traceback (most recent call last):   File "<stdin>", line 1, in ? OverflowError: integer exponentiation


Python 2.2

>>> 9999 ** 8 99920027994400699944002799920001L


Removing the error was the first phase. The next step involved bit-shifting; it used to be possible to left-shift bits out of the picture (resulting in 0):

>>> 2 << 32 0


In 2.3 such an operation gives a warning, but in 2.4 the warning is gone, and the operation results in a real (long) value:

Python 2.3

>>> 2 << 32 __main__:1: FutureWarning: x<<y losing bits or changing sign will return a long in Python 2.4 and up 0


Python 2.4

>>> 2 << 32 8589934592L


Sooner or later (probably later), there will no longer be a long type (at least not at the user level). Things will all happen quietly under the covers. Of course, those with C access will be able to enjoy both types as before, meaning, however, that your C code will still need to be able to distinguish between the different Python integer types. You can read more about the unification of integers and longs in PEP 237.



Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net