Operators & Math
This lesson builds the app's tip calculator — the money-math heart of the Bill Splitter. Money means decimals, and decimals raise a question everyone hits: do I want to round the number, chop it, or just display it differently?
We'll start from whole-number arithmetic and work up to that, then finish with comparisons — the operators that turn numbers into decisions.
Python is a calculator
Nothing surprising yet. Everything stays a clean whole number.
Three kinds of division
Division is where decimals first appear, and Python gives you three tools depending on what you actually want:
print(17 // 5)% is worth dwelling on: 17 % 5 is 2 because 5 goes into 17 three times with
2 left over. It's how you test whether something divides evenly — n % 2 == 0
means "n is even" — and you'll use it constantly.
Note that / gives a float even when the division is exact: 10 / 2 is 5.0,
not 5.
Order of operations
Normal maths rules apply — *, /, //, % before + and -. Parentheses
cost nothing and remove all doubt:
Floats have a rough edge
A number with a decimal point is a float, and any expression involving one produces one. But floats are stored in binary, and some perfectly ordinary decimals have no exact binary form:
print(0.1 + 0.2 == 0.3)This isn't a Python bug — every language using binary floats does it. It's why you
never compare money with ==, and why a receipt needs deliberate rounding.
Round, chop, or just display?
When a number has decimals there are four different things you might want, and they are genuinely different:
| You want… | Use | Result |
|---|---|---|
| the exact number | just use it | 16.799999999999997 |
| a rounded number | round(x, n) |
16.8 |
| the whole part | int(x) |
16 |
| a nice string | f"{x:.2f}" |
"16.80" |
The mental model: round() and int() change the value. An f-string spec
only changes the display — the stored number keeps full precision.
Step through it and watch which names hold numbers and which hold text:
Two more that catch people out:
Percentages
A percentage is "multiply, then divide by 100". An 18% tip on an $84 bill:
Use a variable for the percent so any rate works:
Updating a variable using itself
A common slip is expecting the shorthand to work before the variable exists:
NameErrorRun it and read the error. Then give the code what it's missing so it prints 10.
Comparing values
Arithmetic makes numbers. Comparison turns numbers into True / False —
which is what every decision in a program is built from. There are six:
= assigns a value; == asks a question. Mixing them up is a rite of passage.
Comparisons work on text too, and text comparison is case-sensitive:
print("Cafe" == "cafe")Combining comparisons
and, or and not let you ask compound questions:
andis True only when both sides are.oris True when at least one side is.notinverts.
You'll use these constantly from the next lesson onward, where they decide which branch of your code runs.
🎯 Your turn
Write calculate_tip(bill, tip_percent) — it returns the tip in dollars, rounded
to two decimals:
calculate_tip(100, 18)→18.0calculate_tip(84, 20)→16.8
Hint — round(bill * tip_percent / 100, 2).
Then press ▶ Run, tap the Live App try chips to call it with different inputs, and hit ✓ Check. Green = this piece of the app is built. ✅
