Skip to the workspace
Pebellslearn by building
Fundamentals  ›  Lesson

Operators & Math

Operators 18 minOperators are symbols that combine or compare values, such as +, //, % and ==.

Operators are symbols that combine or compare values, such as +, //, % and ==.

Calculate totals and shares, understand evaluation order and use remainders to account for values that do not divide evenly.

Sharing 17 items among five people can ask for a fractional share, whole items per person, or leftovers. Those are three different results. Use the examples to choose an operation from the question and its units, then check what changes at negative values and decimal boundaries.

17 samosas shared by 5 friends, shown three ways: slash gives 3.4 each, double slash gives 3 each, and percent shows the 2 samosas left on the plate

Share samosas among friends

Five friends buy 17 samosas. "How many each?" has three different answers, depending on the question you are really asking:

python

The common slip is reaching for / out of habit when you need a count:

What does this print?
samosas, friends = 17, 5
each = samosas / friends
print(f"Everyone gets {each} samosas")

% answers more questions than it looks. n % 2 == 0 means "n is even", and minutes % 60 turns 135 minutes into the 15 past the hour:

python

Preparing this topic’s interactive investigation…

Add a tip to the bill

The restaurant bill is ₹840 and you want to add a 10% tip. A percentage is "multiply by the percent, then divide by 100", and the tip is a percent of the bill:

broken — fix it

The total should be ₹924, but it comes out as 840.1. Fix the tip part of the sum.

No brackets were needed, because Python follows school maths: ** first, then * / // %, then + -. Within the latter two groups, operators associate left to right; powers associate right to left. When in doubt, add brackets anyway: they cost nothing and they show your intent.

Splitting a bill brings decimals, and decimals are stored in binary. Many ordinary decimals have no exact binary form, so tiny errors creep in:

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

So you decide what you want from a messy decimal, on purpose. There are three different tools, and they are not interchangeable:

python

round() and int() give you a new number. A format spec like :.2f only changes what is shown.

Step through it and watch each name get its own value while share itself never changes:

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

Running totals use a shortcut. total += 50 means total = total + 50, and there are -=, *=, /= versions too. The name must exist first:

broken — fix itNameError

Run it and read the error. Give total a starting value so it prints 924.

Check a train booking

A booking app decides the fare. Children from 5 up to (not including) 12 pay half, and lower berths go to seniors first. Each comparison gives True or False:

python

= stores a value; == asks "are these equal?". And there is a mistake that looks like plain English and quietly lets everyone through:

broken — fix it

An upper berth (UB) should not count as lower or side-lower. Make the check print False.

When you check for "nothing", use is: seat is None. is asks "the very same object?" rather than "equal value?", and None is one single object.

Traps worth knowing

python

The bitwise operators (&, |, ^, ~, <<, >>) work on the binary digits of whole numbers. You'll rarely need them outside flags and puzzles.

Quotient and remainder must agree

For integers a and nonzero b, a // b rounds the quotient down toward negative infinity. The remainder completes the identity a == (a // b) * b + (a % b):

a b a // b a % b Rebuild a
7 3 2 1 2 × 3 + 1 = 7
-7 3 -3 2 -3 × 3 + 2 = -7
7 -3 -3 -2 -3 × -3 - 2 = 7

This explains why int(-7 / 3) gives -2 but -7 // 3 gives -3: truncation toward zero and floor are different operations. With a positive divisor, the remainder is at least zero and less than the divisor. That makes % 60 useful for seconds left after taking whole minutes.

python

Check units before checking operators

If a bill is 80 currency units and a tip is 15 percent, first convert 15 / 100 to the rate 0.15. The tip is 80 * 0.15 = 12 currency units. Adding 15 directly to 80 mixes a percentage with an amount, even though Python accepts the numbers. Names such as tip_percent, tip_rate and tip_amount make this error visible before you run anything.

/ returns a float for integer operands; it does not promise an exact decimal answer. Rounding a result and formatting it are separate decisions. Integer cents or Decimal constructed from decimal text can support explicit money rules; repeated round calls are not a universal fix for accounting.

Transfer this to packing: 53 items, 8 per box. Derive the full boxes, leftovers and boxes needed if a partial box also counts. Reconstruct the item count to check your answer, then test 0 and 56 items. Parentheses make intent explicit; powers associate right to left, so 2 ** 3 ** 2 means 2 ** (3 ** 2).

Quick practice

What does this print?
print(2 + 3 * 4 ** 2)
What does this print?
print(10 / 5, 10 // 4, 10 % 4)
broken — fix itSyntaxError

Run it and read the error. Add one character so it checks the total.

What does this print?
print(3 < 5 > 4)

🎯 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: "18 percent of the bill" is bill * tip_percent / 100. Wrap it in round(..., 2).

Press ▶ Run, try the Live App chips, then ✓ Check.

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.
Project context and further resources
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.
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.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.