There are four numeric types: plain integers, long integers,
floating point numbers, and complex numbers.
Plain integers (also just called integers)
are implemented using long in C, which gives them at least 32
bits of precision. Long integers have unlimited precision. Floating
point numbers are implemented using double in C. All bets on
their precision are off unless you happen to know the machine you are
Complex numbers have a real and imaginary part, which are both
implemented using double in C. To extract these parts from
a complex number z, use
Numbers are created by numeric literals or as the result of built-in
functions and operators. Unadorned integer literals (including hex
and octal numbers) yield plain integers. Integer literals with an
"L" or "l" suffix yield long integers
("L" is preferred because "1l" looks too much like
eleven!). Numeric literals containing a decimal point or an exponent
sign yield floating point numbers. Appending "j" or
"J" to a numeric literal yields a complex number.
Python fully supports mixed arithmetic: when a binary arithmetic
operator has operands of different numeric types, the operand with the
``smaller'' type is converted to that of the other, where plain
integer is smaller than long integer is smaller than floating point is
smaller than complex.
Comparisons between numbers of mixed type use the same rule.2.3 The functions int(), long(), float(),
and complex() can be used
to coerce numbers to a specific type.
All numeric types support the following operations, sorted by ascending priority (operations in the same box have the same priority; all numeric operations have a higher priority than comparison operations):
||sum of x and y|
||difference of x and y|
||product of x and y|
||quotient of x and y||(1)|
||absolute value or magnitude of x|
||x converted to integer||(2)|
||x converted to long integer||(2)|
||x converted to floating point|
||a complex number with real part re, imaginary part im. im defaults to zero.|
||conjugate of the complex number c|
||x to the power y|
||x to the power y|
[1, 2]is considered equal to
[1.0, 2.0], and similar for tuples.