Pebellslearn by building
0%
RoadmapMatrixCatalogSandboxSign in
Fundamentals  ›  Lesson

Operators & Math

Operators 12 minDoing arithmetic and comparing values
You're building a piece ofBill Splitter & Tip Calculator
This piece — calculate_tip(): Works out the tip in dollars from the bill and a percent.
Scenario The bill is $84 and the party taps the '20%' tip button. The app needs the tip in dollars: $16.80.
Your task
Build calculate_tip(bill, tip_percent). It returns the tip in dollars, rounded to two decimals. Example: calculate_tip(100, 18) → 18.0.
Additional resources

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

python

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:

What does this print?
print(17 // 5)
python

% 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:

python

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:

What does this print?
print(0.1 + 0.2 == 0.3)
python

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:

python
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:

step through it
1price = 16.799999999999997
2rounded = round(price, 2)
3chopped = int(price)
4shown = f"{price:.2f}"
5print(rounded, chopped, shown)

Two more that catch people out:

python

Percentages

A percentage is "multiply, then divide by 100". An 18% tip on an $84 bill:

python

Use a variable for the percent so any rate works:

python

Updating a variable using itself

python

A common slip is expecting the shorthand to work before the variable exists:

broken — fix itNameError

Run 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:

python

= 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:

What does this print?
print("Cafe" == "cafe")

Combining comparisons

and, or and not let you ask compound questions:

python
  • and is True only when both sides are.
  • or is True when at least one side is.
  • not inverts.

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.0
  • calculate_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. ✅

calculate_tip.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
calculate_tip(bill, tip_percent) → floatTip in dollars for a bill and a percent.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.