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")
Area: 3.125 square meters
Edge all around: 7.5 metersThe 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:
>>> 7 / 2
3.5
>>> 6 / 3
2.0
>>> 1.5 + 1.5
3.02.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)
2.5
2 5Floats 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:
>>> 0.1 + 0.2
0.30000000000000004That 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.