Lesson 3 of 14 · 7 min

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.

  1. Work out total.
  2. Work out change from a 10 note.
  3. Print exactly 3 loaves cost $4.50, change $5.50 using an f-string, with 2 decimal places.
main.py
price = 1.5
loaves = 3

# work out total and change, then print the receipt

Python 3.14 runs right here in your browser. The first run downloads about 10 MB; after that it is instant.

Discussion

Stuck? Ask here. Be kind, and share hints rather than full answers.

Loading the discussion…