Operators and expressions
₹1,100 for 18 kilos or ₹900 for 14? Two traders disagree. Rahat's program could settle it — if it would run.
- Guess, then check, six operators
- Build a price-per-kilo calculator
- Fix a NameError from one missing letter
Operators and expressions
₹1,100 for 18 kilos or ₹900 for 14? Two traders disagree. Rahat's program could settle it — if it would run.
- Guess, then check, six operators
- Build a price-per-kilo calculator
- Fix a NameError from one missing letter
Story
pric in one line and price in another, and it will not run at all.Learn it
Python can do any arithmetic a trader does in his head, faster and without slips. Each operator does one job (add, divide, find a remainder) and an expression combines values and operators into a single answer. Two things trip beginners: Python follows an order (multiplication and division before addition and subtraction), and a single misspelled name stops the program with a NameError.
Words to know
a symbol that does something to values: + - * /.
whole-number division: 17 // 5 is 3.
the remainder: 17 % 5 is 2.
a number multiplied by itself, written with two stars: 3 ** 2 is 9.
anything Python works out to one value, like total / weight or 2 + 3 * 4.
* and / run before + and -. Brackets set your own order.
rounds a number to 2 decimal places.
Python has no box with that name, nearly always a spelling mistake.
Step by step: sharing walnut sacks
A family has 23 sacks of walnuts to share between 4 brothers, and each sack sells for ₹1,250.
sacks = 23
brothers = 4
print(sacks // brothers) # each brother gets 5 whole sacks
print(sacks % brothers) # 3 sacks are left over
print(sacks * 1250) # ₹28,750 in all
print(round(28750 / 4, 2)) # ₹7187.5 each, if sold first
//gives the whole sacks each: 5.%gives what is left over: 3.*gives the total value: ₹28,750./always gives a decimal (a float): 7187.5.round()keeps it tidy.- Order:
2 + 3 * 4is 14, because*goes first;(2 + 3) * 4is 20.
Typing numbers in: float(input(...)) turns a typed answer into a decimal number. Type 1250, not
1,250: the comma confuses Python.
Careful: % does not mean percent in Python. It gives the remainder: 17 % 5 is 2.
And // is not a comment; a comment starts with #. Rounding makes an answer easier to read,
not more correct.
Your missions
03-unit-price.pyThe shell as a calculator
7 min- 1
Copy these six into your notebook and guess each answer first. Then type each into the shell and write the real answer beside your guess. Two will surprise you — they're the ones worth remembering.
1100 / 18 1100 // 18 1100 % 18 2 + 3 * 4 (2 + 3) * 4 3 ** 2
The calculator
8 min- 2
In the editor:
total_price = float(input("Price of the whole box in rupees: ")) weight = float(input("Weight of the box in kg: ")) price_per_kg = total_price / weight print("Price per kg:", round(price_per_kg, 2)) - 3
Run it for 1100 and 18, then 900 and 14. Which box is cheaper per kilo?
The typo
3 min- 4
Change
total_pricein line 3 tototal_pricand run:NameError: name 'total_pric' is not defined. Did you mean: 'total_price'?Python guesses — but you make the fix. Fix it and save.
Stuck?
ValueError? Type 1100, not 1,100. ZeroDivisionError? The weight was 0. Answer
looks wrong? * and / run before + and - — add brackets.
Ask for two boxes and print both prices per kilo and the difference. (Printing which is cheaper needs a decision — next lesson.)
No laptop today?
Work out, with the type of each answer: 7 + 2 * 3 · (7 + 2) * 3 · 9 / 3 · 9 // 4 ·
9 % 4 · round(61.555, 1). Then trace the four-line calculator for 500 and 8.
🕌 From our heritage
A calculator for a market is not a small subject. Around 990 CE Abū al-Wafāʾ al-Būzjānī wrote A Book on What Is Necessary from the Science of Arithmetic for Scribes and Businessmen — arithmetic written not for mathematicians but for the people doing daily accounts. He knew the Indian numerals well, yet wrote every number out in words, because his readers did not use the numerals yet. He wrote for the people who would actually use it.
Abū al-Wafāʾ al-Būzjānī (940–998), Kitāb fī mā yaḥtāj ilayhi al-kuttāb wa'l-ʿummāl min ʿilm al-ḥisāb.
Debate it
Rahat's program gives 61.111111111111114. The trader says "sixty-one".
Is the program more accurate than the trader, or only more precise? Where would those
extra digits matter, and where would quoting them make you look foolish? In Unit 3 you
will meet the same question about an AI's confidence score of 0.8734.
Check yourself · practice, not a test
In the app, students answer these one at a time and each answer is checked straight away. It's practice, never a mark — anything they miss comes back later for another go.
1.What does 7 + 2 * 3 give?
- A27
- B13
- C21
- D23
2.What does this print? python boxes = 17 print(boxes // 5, boxes % 5)
- A3.4 0
- B3 2
- C3 5
- D2 3
3.To get a price to two decimal places, use the function ______ .
4.Find the bug. python weight = float(input("Weight: ")) print(total / wieght)
- Afloat should be int
- Bwieght is misspelled, so Python raises a NameError
- Cprint needs quotes
- DDivision needs brackets
5.input() can be used directly in a division, without converting it first.
TrueFalse6.Which of these are expressions? (choose all that apply)
- A7 + 2
- Bprice_per_kg
- Cround(4.567, 1)
- Dprint
7.Put these lines in the order they must appear, so the program prints a price per kilo rounded to two places:
- 1per_kg = total / weight
- 2print("Price per kg:", round(per_kg, 2))
- 3weight = float(input("Weight in kg: "))
- 4total = float(input("Price of the box: "))
An expression is worked out into one value. * and / go first; brackets decide the rest.
Typing practice
Today's symbols are the arithmetic operators: + - * / and the brackets again.
*is Shift + 8, not the letter x./is the key left of the right Shift.- typing.com: the number row and symbols lessons.
- Then type this line four times, with no mistakes:
total = (price * weight) / 100
Watch your fingers on * and (. Both need Shift, and both are in every program you
write from here on.
- +
- -
- *
- /
- *
- /
- total = (price * weight) / 100
- *
- (
Written and designed by Faizan Ayoub. © 2026 Faizan Ayoub. All rights reserved.