Variables and data types
Rahat can see the 12 on the screen. Python still refuses to add 1 to it. Why?
- Store values in named boxes
- Tell str, int and float apart
- Escape the input trap with int()
Variables and data types
Rahat can see the 12 on the screen. Python still refuses to add 1 to it. Why?
- Store values in named boxes
- Tell str, int and float apart
- Escape the input trap with int()
Story
TypeError: can only concatenate str (not "int") to str
Learn it
A variable is a box with a name on it: weight = 12 puts 12 into a box called weight. Every value
also has a type, and the type decides what Python will do with it. 12 is a whole number, 85.50
has a decimal point, "Ambri" is text. The trap: input() always hands back text, even when someone
types digits, so Python refuses to add 1 to it until you convert it.
Words to know
a named box holding a value. = means put into, not is equal to.
a whole number, like 12.
a number with a decimal point, like 85.50.
(string) — text, in quotes, like "Ambri". Digits in quotes, like "12", are text too.
tells you which type is in the box: <class 'int'>.
turn text into a number you can calculate with.
the types don't fit the operation, like adding a number to text. Python refuses rather than guessing.
Step by step: the input trap and its fix
boxes = input("How many boxes? ")
print(boxes * 2)
- Type
6. You might expect12, but you get66:boxesholds the text"6", and text × 2 repeats it. - Check the type:
print(type(boxes))says<class 'str'>. - Fix it:
boxes = int(input("How many boxes? ")). Nowboxesholds the number 6. - Run again:
12. - Decimals need
float: for a price like85.50, usefloat(input(...)). Typing85.50intoint(...)gives a ValueError.
Naming rules: use underscores, never spaces (price_per_kg), and remember capitals count: Weight and
weight are two different boxes. A new value replaces the old one: after weight = weight + 3, the old
value is gone.
Careful: = does not mean "equals" in Python. It means put into: weight = 12 puts 12
into the box. In Lesson 5.4 you meet ==, which really does ask "are these equal?", and mixing
the two up is the commonest mistake in the language.
Your missions
02-apple-price.py before you startThree 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?
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.Only you can see this - 5
Try
print(kg + "1"). You get121— two pieces of text joined. - 6
Fix it:
kg = int(input("How many kilos? ")). Nowkg + 1gives13.
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.
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.*
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
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.After weight = 12, what does type(weight) report?
- A<class 'str'>
- B<class 'int'>
- C<class 'float'>
- D<class 'number'>
2.If a person types 45 when asked by input(), Python stores it as a number.
TrueFalse3.What does this print? python kg = "7" print(kg + kg)
- A14
- B77
- C7 7
- DA TypeError
4.Find the bug. This should print 13, but gives a TypeError: python kg = input("Kilos: ") print(kg + 1)
- Aprint is spelled wrong
- BThe input must be converted: kg = int(input("Kilos: "))
- C1 should be in quotes
- DA 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
- float
- int
- str
7.Put these lines in the order they must appear, so the program asks for a weight and then doubles it correctly:
- 1kg = float(kg)
- 2kg = input("How many kilos? ")
- 3print(kg * 2)
A variable is a named box. input always gives text — convert it before you count with it.
Typing practice
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
- _
- =
- _
- =
- price_per_kg
- price per kg
- price_per_kg = 85.50
Written and designed by Faizan Ayoub. © 2026 Faizan Ayoub. All rights reserved.