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. Bilal'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.
Bilal, 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.
Warm up your fingers
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.
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.
Now you know
- An operator does something to values. An expression works out to one value.
- / always gives a float.
//keeps the whole part;%gives the remainder. - *** and / come before + and -.** Brackets decide the order yourself.
- float(input(...)) turns a typed answer into a decimal number.
- A NameError means there's no box with that name — nearly always a spelling mistake.
Debate it
Bilal'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.