Types of Numeric Data

To enter numeric data inside an interactive python session or in a script, simply set the value of a variable to the desired number. To specify a floating point number, either include a decimal point somewhere in the number, or use exponential notation, for example 1e3 or 1E3 to represent 1000 (1 times 10 to the third power). Note that when using exponentional notation, numbers are always stored as floating point, even if there is no decimal point.

Long integers can be entered by following an ordinary integer with the letter ``L'',
either lowercase (*e.g.* `27l`) or uppercase (*e.g.* `27L`).
(Since a lowercase ``l'' looks so much like the number ``1'', you may want
to get in the habit of using uppercase ``L''s in this context.)
In python, long integers are actually what are sometimes called ``arbitrary
precision'' integers, since they can have as many digits as you have the patience
to type into the computer. On most computers, ordinary integers have a range
from about -2 billion to +2 billion. Trying to use an integer larger than this
value results in an OverlowError:

>>> x = 2000000000 >>> x = x + x / 2 Traceback (innermost last): File "<stdin>", line 1, in ? OverflowError: integer additionYou'll never see such an error when using a long integer:

>>> x = 2000000000L >>> x = x + x / 2 >>> x 3000000000L

A further advantage of long integers is that all arithemetic performed with long integers will be exact, unlike floating point numbers which have a limited precision (about 15 or 16 digits on most computers). It comes as a surprise to many people that adding a small floating point number to a very large floating point number will not change the original floating point number. (In the following example, I'll use some formatted I/O features explained more fully in Section 5.2):

>>> x = 1e16 >>> '%f' % x '10000000000000000.000000' >>> x1 = x + 1. >>> '%f' % x1 '10000000000000000.000000'The addition of 1 to such a huge number makes no difference because of the limited precision of floating point numbers. However, all integer arithmetic is exact when using long integers:

>>> xl = long(x) >>> xl 10000000000000000L >>> xl + 1 10000000000000001L(As explained in Section 3.2, the long function converts its argument into a long integer.) While it might be tempting to store all integers as long integers, remember that, while regular integer arithmetic is supported by most operating systems, python has to perform all its own long integer arithmetic, so using long integers will definitely slow your programs down. But if you know your numbers will fall outside the range of a normal integer, long integers can be a very useful tool.

Complex numbers can be entered into python using either the `complex`
function, or by denoting the complex number as the real portion followed by
a plus sign, followed by the imaginary portion with a trailing ``J'' (either
upper or lower case). There can be no spaces between the imaginary portion and
the ``J''. Thus `3.2 + 7j` is a valid complex number, but
`3.2 + 7 j` is not. The `complex` function can be called with
either one or two arguments representing the real component or the real and
imaginary components respectively; `complex(3.2,7)` is equivalent to the
previous example. Regardless of how you enter a complex number, both components
are stored as floating point numbers.