Python Arena
Step 5 of 5

Floating point numbers

Whole numbers aren't enough for prices, measurements or averages. A number with a decimal point, such as 2.5, -0.75 or 3.0, is a floating point number, or a float for short. Python's name for the type is float.

Floats work in calculations just like integers do. This program works out the size of a rug, in meters:

length = 2.5
width = 1.25
print(f"Area: {length * width} square meters")
print(f"Edge all around: {2 * (length + width)} meters")
Output
Area: 3.125 square meters
Edge all around: 7.5 meters

The parentheses matter. Multiplication comes before addition, so without them Python would work out 2 * length first and then add width to it, once instead of twice.

Division gives a float

The result of / is always a float, even when the numbers divide evenly:

In the Python interpreter
>>> 7 / 2
3.5
>>> 6 / 3
2.0
>>> 1.5 + 1.5
3.0

2.0 is the same amount as 2, but it's a float, so Python shows it with a decimal point. The next module looks at the types of results more closely.

A point, not a comma

Python writes decimals with a point. A comma separates values, so print(2,5) prints two numbers, not two and a half:

print(2.5)
print(2,5)
Output
2.5
2 5

Floats aren't always exact

A computer stores a float in binary with a fixed number of digits, so most decimal fractions are stored as a very close approximation. Usually you never notice, but now and then it shows in the last digit:

In the Python interpreter
>>> 0.1 + 0.2
0.30000000000000004

That isn't a mistake in your program: almost every programming language does the same. A later module shows how to round numbers when you print them.

Your task

Challenge 1 of 3

Fill in the blank so the program prints the average of the two scores:

Average: 8.25

Challenge 2

What does this program print? Type each line it prints.

Challenge 3

A runner's three lap times, in seconds, are in variables. Add lines that calculate the total time and the mean lap time from the variables and print them with f-strings:

Total: 187.5 seconds
Mean: 62.5 seconds