Floats
In this tutorial, we'll use decimal numbers and understand precision.
A float is a number with a decimal point.
price = 4.99
height = 1.75
temperature = -2.5
Floats are used for measurements, money, averages, percentages, and calculations where whole numbers are not enough.
Float Arithmetic
You can use floats in calculations:
price = 4.99
quantity = 3
total = price * quantity
print(total)
Output:
14.97
Python can combine integers and floats. The result is usually a float.
Rounding
Some decimal results are long:
average = 10 / 3
print(average)
You can round them:
print(round(average, 2))
Output:
3.33
The 2 means "round to 2 decimal places".
Floating Point Surprises
Computers cannot represent every decimal number perfectly. You may see this:
print(0.1 + 0.2)
Output might be:
0.30000000000000004
This is normal in many programming languages. For beginner work, use round() when displaying results.
Money Warning
Floats are fine for simple beginner examples, but real money systems often use special decimal tools to avoid rounding problems.
For now, it is okay to write:
price = 19.99
tax = price * 0.2
print(round(tax, 2))
Practice
Create three test scores with decimals. Calculate and print the average rounded to one decimal place.