Numeric Type Functions

Python currently supports different sets of built-in functions for numeric types. Some convert from one numeric type to another while others are more operational, performing some type of calculation on their numeric arguments.


The int(), long(), float(), and complex() built-in functions are used to convert from any numeric type to another. Starting in Python 1.5, these functions will also take strings and return the numerical value represented by the string.
The following are some examples using the numeric type conversion built-ins:

>>> int(4.25555)
>>> long(42)
>>> float(4)
>>> complex(4)
>>> complex(2.4,-8)
>>> complex(2.3e-10,45.3e4)

nutshells these numeric type conversion built-in functions

Function Operation
~ num

converts string or number obj to (plain) integer; provides same behavior as string.atoi();
long( obj, base=10 )converts string or number obj to long integer; provides same behavior as string.atol();
float( obj )

converts string or number obj to floating point; provides same behavior as string.atof()
complex( str ) or complex( real, imag =0.0)

converts string str to complex, or takes real (and perhaps imag inary) numbers and returns a complex number with those components

You may also like...