Numbers and Maths
Add up orders, discounts and totals.
It is Friday night, and Ava has three orders to total. Two monstera plants, one fern, and a delivery fee. Last week she used a calculator, and made a mistake. This time, she asks Python. Python is a very powerful calculator, and it never gets tired, and never mistypes.

- The task: total up three orders
- The old way: a calculator, and a mistake
- The new way: let Python do the maths
The arithmetic operators
Python has the usual operators. Plus adds, minus subtracts, star multiplies, and slash divides. Two stars raise a number to a power. Double slash gives whole number division, and the percent sign gives the remainder, called modulo. Slash always returns a float, even when the answer looks whole.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | add | 7 + 2 | 9 |
| - | subtract | 7 - 2 | 5 |
| * | multiply | 7 * 2 | 14 |
| / | divide | 7 / 2 | 3.5 |
| // | whole division | 7 // 2 | 3 |
| % | remainder | 7 % 2 | 1 |
| ** | power | 7 ** 2 | 49 |
Try the operators
Let's try them in the REPL. Seven plus two is nine. Seven divided by two is three point five, but with double slash, it is three. The remainder is one. Seven squared is forty nine. And Python follows the usual order of operations, so multiplication happens before addition, and brackets change the order.
Ava totals an order
Now the real problem. Two monsteras at twenty four ninety nine, one fern at nine fifty, and a five dollar delivery fee. We store each price, then calculate the subtotal, then add the delivery to get the total. And the answer appears, without a calculator.
monstera = 24.99
fern = 9.50
delivery = 5
subtotal = 2 * monstera + fern
total = subtotal + delivery
print("Subtotal:", subtotal)
print("Total:", total)Subtotal: 59.48 Total: 64.47999999999999
Discounts and percentages
Ava offers ten percent off. To find the discount, multiply the total by point one. To get the new price, subtract it. And notice the round function, which keeps the money to two decimal places. Computers store decimals in binary, so tiny rounding errors can appear, and rounding is the cure.
total = 59.48
discount = total * 0.10
final = total - discount
print("Discount:", round(discount, 2))
print("You pay:", round(final, 2))
print(0.1 + 0.2)Discount: 5.95 You pay: 53.53 0.30000000000000004
Packing boxes: // and %
Here is where double slash and the percent sign shine. Ava has thirty seven plants, and each box holds six. Double slash tells us how many full boxes, six. And the remainder tells us how many plants are left over, one. Division and remainder, together, solve many real problems.
plants = 37
per_box = 6
full_boxes = plants // per_box
left_over = plants % per_box
print("Full boxes:", full_boxes)
print("Left over:", left_over)Full boxes: 6 Left over: 1
Shortcuts and built-in helpers
Python has shortcuts. Plus equals adds to a variable, and minus equals subtracts. And useful helpers are built in. Abs gives the absolute value, min and max pick the smallest and largest, and round tidies a decimal. For more maths, import the math module, which brings square roots, and the ceiling function, to round up.
import math
stock = 40
stock -= 3
stock += 10
print(stock)
print(max(3, 9, 5), min(3, 9, 5))
print(abs(-7), round(2.567, 1))
print(math.sqrt(81), math.ceil(4.2))47 9 3 7 2.6 9.0 5
Cost of a bouquet
Your turn. A bouquet has five roses at two dollars forty each, and costs three dollars for wrapping. Calculate the total, and print it. Ready? Here is one solution. Multiply, add the wrapping, and print. Fifteen dollars, exactly.
Five roses cost 2.40 each, wrapping costs 3. Print the total.
Show hint
roses * price + wrapping
Show solution
roses = 5
price = 2.40
wrapping = 3
total = roses * price + wrapping
print("Total:", total)Total: 15.0
Summary
Excellent. You now use Python as a calculator. You know plus, minus, star, slash, double slash, percent, and power. You know to use round for money, and the math module for more. Next, Ava writes the thank you notes, which means working with text.
Key takeaways- Operators: + - * / // % **
- round() tames decimals
- // and % solve packing problems