GNU Info

Info Node: (python2.1-lib.info)Numeric Types

(python2.1-lib.info)Numeric Types


Next: Sequence Types Prev: Comparisons Up: Built-in Types
Enter node , (file) or (file)node

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.(1) 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      (2)
                         integer                  
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 %{}  (3)
                         Y)'                      
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
     the `math'  module for well-defined conversions.

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

Bit-string Operations
---------- Footnotes ----------

(1)  As a consequence, the list `[1, 2]' is considered equal to `[1.0,
2.0]', and similar for tuples.


automatically generated by info2www version 1.2.2.9