Lesson 3 · maths and f-strings
Adding up a bill
- 1. Story
- 2. Guess
- 3. Your turn
- 4. Catch Sitara’s mistake
EllaTwo lattes and a sandwich. Kai is counting on his fingers again.
KaiI am not! I am… checking twice.
SitaraLet Python do the sums, and print a friendly bill.
Python can do maths, and an f-string puts a value inside text.
- 1. * multiplies and + adds
- 2. f"…{name}…" puts the value of name inside the text
- 3. Python does the maths first, then prints
cafe.py
Try: Change scone to 5. What is the total now?
Read · about 1 minute
The basics, in plain words
Python can do maths, and an f-string puts a value inside text.
latte = 4
scone = 3
total = latte + 2 * scone
print(f"Total: ${total}")- * multiplies and + adds
- f"…{name}…" puts the value of name inside the text
- Python does the maths first, then prints
- + - * /
- Add, take away, multiply and divide. Python follows maths order, so * comes before +.
- f"…{x}…"
- An f-string. Put f before the quotes and a name inside { } to show its value in the text.
Common slip: Leaving out the f. Without it, Python prints {total} as plain words.
Read all twelve Python basics with Sitara →