We can convert a number from one type to another. A conversion may involve a loss of precision because we've reduced the number of bits available. A conversion may also add a false sense of precision by adding bits which don't have any real meaning.
We'll call these factory functions because they are a factory for creating new objects from other objects. The idea of factory function is a very general one, and these are just the first of many examples of this pattern.
Also, this section is only an introduction. A more complete list of conversion functions is provided in the section called “String Conversion Functions”.
There are a number of conversions from one numeric type to another.
int (x) →
integerGenerates an integer from the object
x. If x is a
floating point number, digits to the right of the decimal point
are truncated as part of creating an integer. If the floating
point number is more than about 10 digits, a long integer object
is created to retain the precision. If
x is a long integer that is too large
to be represented as an integer, there's no conversion. If
x is a string, the string is parsed to
create an integer value. Complex values can't be turned into
integers directly.
float (x) →
floatGenerates a float from object x.
If x is an integer or long integer, a
floating point number is created. Note that long integers can have
a large number of digits, but floating point numbers only have
approximately 16 digits; there can be some loss of precision.
Complex values can't turned into floating point numbers
directly.
long (x) →
longGenerates a long integer from x.
If x is a floating point number, digits
to the right of the decimal point are truncated as part of
creating a long integer.
complex (real,
[imag]) → complexGenerates a complex number from real and imag.
If this “complex (real,
[imag] )” syntax synopsis is
confusing, you might want to see Syntax For Newbies.
We'll show optional parameters to functions by surrounding them
with []'s. In the case of complex, there will be
two different ways to use the function, with one parameter,
real, or with two parameters,
real, imag. We don't
actually enter the []'s, they're just hints as to what the two forms
of the function are.
In the case of “complex
(real, [imag]
)”, we have two choices.
>>>complex( 2.0, 3.0 )(2+3j)>>>complex( 4.0 )(4+0j)
float (x ) creates a
floating point number equal to the string or number
x. If a string is given, it must be a valid
floating point number: digits, decimal point, and an exponent
expression. Examples: float("6.02E24"),
float(23). This is handy when doing division to prevent
getting the simple integer quotient.
For example:
>>>print float(22)/7, 22/73.14285714286 3
int (x ) creates an
integer equal to the string or number x. If a
string is given, it must be a valid decimal integer string. Examples:
int("1243"), int(3.14159).
long (x ) creates a
long integer equal to the string or number x.
If a string is given, it must be a valid decimal integer. The expression
long(2) has the same value as the literal
2L. Examples: long(6.02E23),
long(2).
For example:
>>>print long(2)**6418446744073709551616
This shows the range of values possible with 64-bit integers, available on larger computers.
Complex is not as simple as the others. A complex number has two parts, real and imaginary. Conversion to complex typically involves two parameters.
complex (r,
[i] ) creates a complex number with
the real part of r; if the second parameter,
i, is given, this is the imaginary part of
the complex number, otherwise the imaginary part is zero. The string
must be a valid complex number. Examples:
>>>print complex(3,2), complex(4), complex("3+4j")(3+2j) (4+0j) (3+4j)
Note that the second parameter, with the imaginary part of the number, is optional. This leads to a number of different ways to call this function. In the example above, we used three variations: two numeric parameters, one numeric parameter and one string parameter.