float

Floating point numbers are supported in Python by the float type. Python floats are implemented as IEEE-754 double-precision floating point numbers with 53 bits of binary precision. This is equivalent to between 15 and 16 significant digits in decimal.

Any literal number containing a decimal point is interpreted by Python as a float:

3.125

Scientific notation can be used, so for large numbers — such as {ParseError: KaTeX parse error: Expected 'EOF', got '}' at position 1: }̲3 imes10^8{/}, the approximate speed of light in metres per second — we can write:

>>> 3e8
300000000.0

and for small numbers like Planck's constant {ParseError: KaTeX parse error: Expected 'EOF', got '}' at position 1: }̲1.616 imes10^} we can enter:

>>> 1.616e-35
1.616e-35

Notice how Python automatically switches the display representation to the most readable form.

As for integers, we can convert to floats from other numeric or string types using the float constructor. For example, the constructor can accept an int:

>>> float(7)
7.0

The float constructor can also accept a string as follows:

>>> float("1.618")
1.618


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset