Class 9 · Unit 5: Python · Lesson 5.2 · 40 min
Variables and data types
Bilal can see the 12 on the screen. Python still refuses to add 1 to it. Why?
Today you will: Store values in named boxes · Tell str, int and float apart · Escape the input trap with int()
Story
Bilal's uncle keeps the orchard accounts on a slate by the door: the variety, the weight of each box, the price he was paid per kilo. Three things, written in three places, every day for a month.
Bilal offers to write a program for it. He types the weight, the price, and then tries to add one more kilo to the weight — and Python refuses. Not a wrong answer. A refusal:
TypeError: can only concatenate str (not "int") to str
Bilal: “It has the number. I can see the number on the screen.”
Hiba reads the message again. "It has the characters. That is not the same thing."
Watch
Boxes with names on them — — scan code 9.5.2 in the printed book.
Warm up your fingers
Today's symbols are the underscore _ and the equals sign =.
_is Shift + hyphen, the key left of=on the top row.- Python variable names cannot contain spaces, so words are joined with underscores:
price_per_kg, notprice per kg. - typing.com: continue the symbols lessons.
- Then type these three lines twice, with no mistakes:
price_per_kg = 85.50
On the laptop
You need: Thonny · save as 02-apple-price.py before you start
Mission 1: Three boxes, three types (6 min)
- ☐1
Type and run:
variety = "Ambri" weight = 12 price_per_kg = 85.50 print(type(variety), type(weight), type(price_per_kg)) - ☐2
You'll see str (text in quotes), int (whole number) and float (decimal point). Change
weightto12.5and run again. What changed?
Mission 2: The trap (9 min)
- ☐3
Add these. Predict: what will
type(kg)say when you type12?kg = input("How many kilos? ") print(type(kg)) - ☐4
Predict: will
print(kg + 1)give13,121, or refuse? Run it:TypeError: can only concatenate str (not "int") to str. - ☐5
Try
print(kg + "1"). You get121— two pieces of text joined. - ☐6
Fix it:
kg = int(input("How many kilos? ")). Nowkg + 1gives13.
Mission 3: Finish it (3 min)
- ☐7
Print the variety, the kilos and the total price,
kg * price_per_kg. Save.
Stuck?
<class 'str'> for a number? That's the trap — wrap input in int(...). ValueError?
You typed 12.5 or twelve; type 12. SyntaxError on step 6? Count the brackets: two
open, two close.
Finished early?
Ask for the price too. What happens if someone types 85.50 into int(...)? One sentence:
why does float(...) exist?
No laptop today?
Draw three labelled boxes — variety, weight, price_per_kg — and fill them for a four-line
program where the last line is weight = weight + 3. What's in weight at the end, and what
happened to the old value?
🕌 From our heritage
intandfloatare two ways of writing a quantity: whole things, and parts of things. In 952 CE in Damascus, al-Uqlīdisī wrote the earliest surviving Arabic book on the Hindu numerals we still use, and in it the earliest known direct treatment of decimal fractions — numbers with a part after the point, and a mark to show where the whole number ends. He also reworked the methods so they could be done with pen and paper instead of a dust board, because he was writing for people who had to calculate at a desk.Abū al-Ḥasan Aḥmad ibn Ibrāhīm al-Uqlīdisī, Kitāb al-Fuṣūl fī al-Ḥisāb al-Hindī*, Damascus, 952/3 CE.*
Now you know
- A variable is a named box.
weight = 12means put 12 into the box called weight. - Three types you will use all year: str (text, in quotes), int (whole number), float (number with a decimal point).
type(x)tells you which type is in the box.- input() always returns a str , even when the person typed digits. Wrap it in
int(...)orfloat(...)when you need to calculate with it. - A TypeError means the types do not fit the operation. Python refuses rather than guessing — and refusing is the safer behaviour.
- Names use underscores, never spaces, and Python tells capitals apart:
Weightandweightare two different boxes.
Debate it
Python could have guessed. It could have looked at "12" + 1, decided you probably meant
thirteen, and given it to you.
It does not. Why might refusing be safer than guessing — for a program that adds up a farmer's money, or one that reads a patient's dose? And where have you seen a machine this year that does guess, and sounds just as confident either way?
Check yourself — practice, not a test
1. After weight = 12, what does type(weight) report?
- ○ <class 'str'>
- ○ <class 'int'>
- ○ <class 'float'>
- ○ <class 'number'>
2. If a person types 45 when asked by input(), Python stores it as a number.
- ○ True
- ○ False
3. What does this print? python kg = "7" print(kg + kg)
- ○ 14
- ○ 77
- ○ 7 7
- ○ A TypeError
4. Find the bug. This should print 13, but gives a TypeError: python kg = input("Kilos: ") print(kg + 1)
- ○ print is spelled wrong
- ○ The input must be converted: kg = int(input("Kilos: "))
- ○ 1 should be in quotes
- ○ A variable cannot be called kg
5. A number with a decimal point, like 85.50, has the type ______ .
6. Match each value to its type:
"Ambri" · 12 · 85.50
Match with: float · int · str7. Put these lines in the order they must appear, so the program asks for a weight and then doubles it correctly:
- 1. kg = float(kg)
- 2. kg = input("How many kilos? ")
- 3. print(kg * 2)
Remember
A variable is a named box. input always gives text — convert it before you count with it.