Subsections


2.1.4 Numeric Types

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 working with.

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 z.real and z.imag.

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):

Operation  Result  Notes 
x + y sum of x and y  
x - y difference of x and y  
x * y product of x and y  
x / y quotient of x and y (1)
x % y remainder of x / y  
-x x negated  
+x x unchanged  
abs(x) absolute value or magnitude of x  
int(x) x converted to integer (2)
long(x) x converted to long integer (2)
float(x) x converted to floating point  
complex(re,im) a complex number with real part re, imaginary part im. im defaults to zero.  
c.conjugate() conjugate of the complex number c  
divmod(x, y) the pair (x / y, x % y) (3)
pow(x, y) x to the power y  
x ** y x to the power y  

Notes:

(1)
For (plain or long) integer division, the result is an integer. The result is always rounded towards minus infinity: 1/2 is 0, (-1)/2 is -1, 1/(-2) is -1, and (-1)/(-2) is 0. Note that the result is a long integer if either operand is a long integer, regardless of the numeric value.

(2)
Conversion from floating point to (long or plain) integer may round or truncate as in C; see functions floor() and ceil() in module math for well-defined conversions.

(3)
See section 2.3, ``Built-in Functions,'' for a full description.


2.1.4.1 Bit-string Operations on Integer Types

Plain and long integer types support additional operations that make sense only for bit-strings. Negative numbers are treated as their 2's complement value (for long integers, this assumes a sufficiently large number of bits that no overflow occurs during the operation).

The priorities of the binary bit-wise operations are all lower than the numeric operations and higher than the comparisons; the unary operation "~" has the same priority as the other unary numeric operations ("+" and "-").

This table lists the bit-string operations sorted in ascending priority (operations in the same box have the same priority):

Operation  Result  Notes 
x | y bitwise or of x and y  
x ^ y bitwise exclusive or of x and y  
x & y bitwise and of x and y  
x << n x shifted left by n bits (1), (2)
x >> n x shifted right by n bits (1), (3)
~x the bits of x inverted  

Notes:

(1)
Negative shift counts are illegal and cause a ValueError to be raised.
(2)
A left shift by n bits is equivalent to multiplication by pow(2, n) without overflow check.
(3)
A right shift by n bits is equivalent to division by pow(2, n) without overflow check.



Footnotes

... rule.2.3
As a consequence, the list [1, 2] is considered equal to [1.0, 2.0], and similar for tuples.


Send comments on this document to python-docs@python.org.