DekGenius.com
[ Team LiB ] Previous Section Next Section

4.3 Numbers

The first object type on the tour is Python numbers. In general, Python's number types are fairly typical and will seem familiar if you've used almost any other programming language in the past. They can be used to keep track of your bank balance, the distance to Mars, the number of visitors to your web site, and just about any other numeric quantity.

Python supports the usual numeric types (known as integer and floating point), as well as literals for creating numbers, and expressions for processing them. In addition, Python provides more advanced numeric programming support, including a complex number type, an unlimited precision integer, and a variety of numeric tool libraries. The next few sections give an overview of the numeric support in Python.

4.3.1 Number Literals

Among its basic types, Python supports the usual numeric types: both integer and floating-point numbers, and all their associated syntax and operations. Like the C language, Python also allows you to write integers using hexadecimal and octal literals. Unlike C, Python also has a complex number type, as well as a long integer type with unlimited precision (it can grow to have as many digits as your memory space allows). Table 4-2 shows what Python's numeric types look like when written out in a program (that is, as literals).

Table 4-2. Numeric literals

Literal

Interpretation

1234, -24, 0

Normal integers (C longs)

9999999999999999999L

Long integers (unlimited size)

1.23, 3.14e-10, 4E210, 4.0e+210

Floating-point (C doubles)

0177, 0x9ff, 0XFF

Octal and hex literals

3+4j, 3.0+4.0j, 3J

Complex number literals

In general, Python's numeric types are straightforward, but a few coding concepts are worth highlighting here:


Integer and floating-point literals

Integers are written as a string of decimal digits. Floating-point numbers have an embedded decimal point, and/or an optional signed exponent introduced by an e or E. If you write a number with a decimal point or exponent, Python makes it a floating-point object and uses floating-point (not integer) math when it's used in an expression. The rules for writing floating-point numbers are the same as in the C language.


Numeric precision and long integers

Plain Python integers (row 1 of Table 4-2) are implemented as C "longs" internally (i.e., at least 32 bits), and Python floating-point numbers are implemented as C "doubles"; Python numbers get as much precision as the C compiler used to build the Python interpreter gives to longs and doubles.[2]

[2] That is, the standard CPython implementation. In the Jython Java-based implementation, Python types are really Java classes.


Long integer literals

On the other hand, if an integer literal ends with an l or L, it becomes a Python long integer (not to be confused with a C long) and can grow as large as needed. In Python 2.2, because integers are converted to long integers on overflow, the letter L is no longer strictly required.


H exadecimal and octal literals

The rules for writing hexadecimal (base 16) and octal (base 8) integers are the same as in C. Octal literals start with a leading zero (0), followed by a string of digits 0-7; hexadecimals start with a leading 0x or 0X, followed by hexadecimal digits 0-9, and A-F. In hexadecimal literals, hex digits may be coded in lower- or uppercase.


Complex numbers

Python complex literals are written as realpart+imaginarypart, where the imaginarypart is terminated with a j or J. The realpart is technically optional, and the imaginarypart can come first. Internally, they are implemented as a pair of floating-point numbers, but all numeric operations perform complex math when applied to complex numbers.

4.3.2 Built-in Tools and Extensions

Besides the built-in number literals shown in Table 4-2, Python provides a set of tools for processing number objects:


Expression operators

+, *, >>, **, etc.


Built-in mathematical functions

pow, abs, etc.


Utility modules

random, math, etc.

We'll meet all of these as we go along. Finally, if you need to do serious number-crunching, an optional extension for Python called NumPy (Numeric Python) provides advanced numeric programming tools, such as a matrix data type and sophisticated computation libraries. Hardcore scientific programming groups at places like Lawrence Livermore and NASA use Python with NumPy to implement the sorts of tasks they previously coded in C++ or FORTRAN.

Because it's so advanced, we won't say more about NumPy in this chapter. (See the examples in Chapter 29.) You will find additional support for advanced numeric programming in Python at the Vaults of Parnassus site. Also note that NumPy is currently an optional extension; it doesn't come with Python and must be installed separately.

    [ Team LiB ] Previous Section Next Section