Float

A number with a decimal point, e.g. 3.14.

A float is a number with a decimal point, used for measurements and any value that isn't whole. The division operator / always produces a float, even for something like 10 / 2. Floats are stored in binary, so a few decimals can't be represented exactly and you may see a tiny rounding error.

Example
price = 3.14
print(price * 2)              # 6.28
print(10 / 2)                 # / always gives a float -> 5.0
print(0.1 + 0.2)              # a tiny rounding error appears
print(round(0.1 + 0.2, 2))    # round for display
Output
6.28
5.0
0.30000000000000004
0.3

Where this shows up in real Python

Floats handle anything with a fractional part: averages, prices, measurements, and percentages.

Commonly used Float tools

  • float('3.14') — parse a decimal string
  • round(x, 2) — round to a number of decimal places
  • abs(x) — drop the sign
  • f'{x:.2f}' — format to a fixed number of decimals
  • import math — math.floor, math.ceil, math.sqrt, …

Related lessons

Related terms