Learnitweb

float values in Python

float (floating point real values) or simply called floats, represent real numbers and are written with a decimal. The left of the decimal represents integer value and the right represents the fractional part.

Floats may also be represented in scientific notation or exponential notation, with e or E indicating the power of 10, for example, 9.99e3 = 9.99 x 102. Scientific notation is very helpful is representing very large values in concise way like distance between sun and earth.

Float values are represented only in decimal form. Binary, octal, hexadecimal forms are not allowed for float data types.

x = 9.99
print(f'value of x is {x}')
print(f'type of x: {type(x)}')

y = 9.
print(f'value of y is {y}')
print(f'type of y: {type(y)}')

z = .99

print(f'value of z is {z}')
print(f'type of z: {type(z)}')

v = 9.99e3
print(f'value of v is {v}')
print(f'type of v: {type(v)}')

Output

value of x is 9.99
type of x: <class 'float'>
value of y is 9.0
type of y: <class 'float'>
value of z is 0.99
type of z: <class 'float'>
value of v is 9990.0
type of v: <class 'float'>

Floating-point numbers are represented in computer hardware as base 2 (binary) fractions. Value printed by the Python looks like the exact value of fraction, the actual stored value in the hardware is the nearest representable binary fraction. You can learn more about this in Python documentation.