15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

5.2. Integers<br />

<strong>Python</strong> has several types of integers. There is the Boolean type with two possible values. There are the<br />

regular or plain integers: generic vanilla integers recognized on most systems today. <strong>Python</strong> also has a<br />

long integer size; however, these far exceed the size provided by C longs . We will take a look at these<br />

types of integers, followed by a description of operators and built-in functions applicable only to <strong>Python</strong><br />

integer types.<br />

5.2.1. Boolean<br />

The Boolean type was introduced in <strong>Python</strong> 2.3. Objects of this type have two possible values, Boolean<br />

TRue and False. We will explore Boolean objects toward the end of this chapter in Section 5.7.1.<br />

5.2.2. Standard (Regular or Plain) Integers<br />

<strong>Python</strong>'s "plain" integers are the universal numeric type. Most machines (32-bit) running <strong>Python</strong> will<br />

provide a range of -2 31 to 2 31 -1, that is -2, 147,483,648 to 2,147,483,647. If <strong>Python</strong> is compiled on a<br />

64-bit system with a 64-bit compiler, then the integers for that system will be 64-bit. Here are some<br />

examples of <strong>Python</strong> integers:<br />

0101 84 -237 0x80 017 -680 -0X92<br />

<strong>Python</strong> integers are implemented as (signed) longs in C. Integers are normally represented in base 10<br />

decimal format, but they can also be specified in base 8 or base 16 representation. Octal values have a<br />

"0" prefix, and hexadecimal values have either "0x" or "0X" prefixes.<br />

5.2.3. Long Integers<br />

The first thing we need to say about <strong>Python</strong> long integers (or longs for short) is not to get them<br />

confused with longs in C or other compiled languagesthese values are typically restricted to 32- or 64bit<br />

sizes, whereas <strong>Python</strong> longs are limited only by the amount of (virtual) memory in your machine. In<br />

other words, they can be very L-O-N-G longs.<br />

Longs are a superset of integers and are useful when your application requires integers that exceed the<br />

range of plain integers, meaning less than -2 31 or greater than 2 31 -1. Use of longs is denoted by the<br />

letter "L", uppercase (L) or lowercase (l), appended to the integer's numeric value. Values can be<br />

expressed in decimal, octal, or hexadecimal. The following are examples of longs:<br />

16384L -0x4E8L 017L -2147483648l 052144364L<br />

299792458l 0xDECADEDEADBEEFBADFEEDDEAL -5432101234L

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!