Numbers

On to the nitty-gritty. The first object type on our tour is Python numbers. In general, Python’s number types are fairly typical and will seem familiar if you’ve used just about any other programming language in the past. Python supports the usual numeric types (integer and floating point), constants, and expressions. 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.

Standard Numeric Types

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

Table 2-2. Numeric Constants

Constant

Interpretation

1234, -24, 0

Normal integers (C longs)

999999999999L

Long integers (unlimited size)

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

Floating-point (C doubles)

0177, 0x9ff

Octal and hex constants

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

Complex number constants

By and large, Python’s numeric types are straightforward, but a few ...

Get Learning Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.