Class 9 · Unit 5: Python · Lesson 5.3 · 40 min
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.
Today you will: Guess, then check, six operators · Build a price-per-kilo calculator · Fix a NameError from one missing letter
Story
At the Sopore mandi, a trader calls out a price for a whole box: eleven hundred rupees, eighteen kilos. The man beside him wants to know whether that is better than the box behind, at nine hundred for fourteen.
Both of them work it out in their heads, in about two seconds, and disagree.
Rahat, who has a program that can divide, says nothing — because his program asks for the price with a variable he has spelled pric in one line and price in another, and it will not run at all.
Watch
Making Python do the arithmetic — — scan code 9.5.3 in the printed book.
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
- Operator a symbol that does something to values:
+-*/. - // whole-number division:
17 // 5is3. - % the remainder:
17 % 5is2. - Power a number multiplied by itself, written with two stars:
3 ** 2is9. - Expression anything Python works out to one value, like
total / weightor2 + 3 * 4. - Order of operations
*and/run before+and-. Brackets set your own order. - round(x, 2) rounds a number to 2 decimal places.
- NameError 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.
On the laptop
You need: Thonny · save as 03-unit-price.py
Mission 1: The 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
Mission 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?
Mission 3: 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.
Finished early?
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
1. What does 7 + 2 * 3 give?
- ○ 27
- ○ 13
- ○ 21
- ○ 23
2. What does this print? python boxes = 17 print(boxes // 5, boxes % 5)
- ○ 3.4 0
- ○ 3 2
- ○ 3 5
- ○ 2 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)
- ○ float should be int
- ○ wieght is misspelled, so Python raises a NameError
- ○ print needs quotes
- ○ Division needs brackets
5. input() can be used directly in a division, without converting it first.
- ○ True
- ○ False
6. Which of these are expressions? (choose all that apply)
- ○ 7 + 2
- ○ price_per_kg
- ○ round(4.567, 1)
7. Put these lines in the order they must appear, so the program prints a price per kilo rounded to two places:
- 1. per_kg = total / weight
- 2. print("Price per kg:", round(per_kg, 2))
- 3. weight = float(input("Weight in kg: "))
- 4. total = float(input("Price of the box: "))
Remember
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.