We have a number of conversion functions in the various types of data we've seen in this chapter. Each of the built-in numeric types has a proper constructor function. As with many Python functions, each of these has a number of different kinds of arguments it can handle:
int()
: Creates anint
from a wide variety of other objectsint(3.718)
for another numberint('48879')
for a string in base 10int('beef', 16)
for a string in the given base—16 in this exampleThe
int()
function can ignore the extra prefix characters on numbers written in Python literal syntax:int('0b1010',2)
,int('0xbeef',16)
, andint('0o123',8)
float()
: Creates afloat
from other objectsfloat(7331)
for another numberfloat('4.8879e5')
for a decimal string
complex()
: Createscomplex
values from a variety of objectscomplex(23)
creates(23+0j)
complex(23, 3)
creates(23+3j)
complex('23+2j')
creates(23+2j)
We can convert single numbers, pairs of numbers, and even some strings into...