Numbers and f-strings
Python is a good calculator:
| Symbol | Meaning | Example | Result |
|---|---|---|---|
+ - |
add, subtract | 7 - 2 |
5 |
* |
multiply | 7 * 2 |
14 |
/ |
divide | 7 / 2 |
3.5 |
// |
divide, whole part only | 7 // 2 |
3 |
% |
remainder | 7 % 2 |
1 |
** |
power | 2 ** 10 |
1024 |
Brackets work as in maths: (1 + 2) * 3 is 9.
f-strings
To put values into text, write f before the quotes and the values inside { }:
item = "milk"
price = 1.2
print(f"One {item} costs {price}") # One milk costs 1.2
print(f"Two cost {price * 2}") # Two cost 2.4
print(f"Price: {price:.2f}") # Price: 1.20
:.2f after a value means "show it with exactly 2 decimal places", which is how money is usually shown.
Your task
A loaf of bread costs 1.5 and you buy 3.
- Work out
total. - Work out
changefrom a 10 note. - Print exactly
3 loaves cost $4.50, change $5.50using an f-string, with 2 decimal places.