Year 8 · Spring 2
Python I · Lesson 2 of 5
Variables, data types and arithmetic
PracticalTopic 13 Programming basics
Year 8 · Python I · Lesson 2 of 5
You will learn to
- Explain what a variable is, and read = as 'becomes'.
- Identify int, float and str values, like 7, 3.5 and "7".
- Use int() to turn typed input into a number before doing maths.
- Use // and % to work out DIV and MOD, like 17 // 5 = 3 and 17 % 5 = 2.
Year 8 · Python I · Lesson 2 of 5
Success criteria
You have got it when you can say:
- I can read score = score + 10 aloud as 'score becomes score plus 10'.
- I can explain why "7" + "7" gives "77", but 7 + 7 gives 14.
- I can build a tiny calculator that adds two numbers the user types.
Year 8 · Python I · Lesson 2 of 5
Retrieval starter
Questions from before. Write your answers, then show them.
- Last lesson
How many times does for i in range(2, 10, 2): run?
A 4B 5C 8
A. 4
- Last unit
What is 1011 in denary?
A 13B 11C 7
B. 11
- Earlier this year
The web and the internet are…
A The same thingB The internet runs on the webC The web is a service that runs on the internet
C. The web is a service that runs on the internet
Year 8 · Python I · Lesson 2 of 5
Key words (1 of 2)
- variable
- A named place in memory that holds a value.
- assignment
- Storing a value in a variable. = in Python, ← in pseudocode.
- concatenation
- Joining strings together: "7" + "7" = "77".
- Casting
- Changing a value from one data type to another, like int("13").
Year 8 · Python I · Lesson 2 of 5
Key words (2 of 2)
- Selection
- Choosing a path with if / else.
- Iteration
- Repeating steps: a loop.
- Boolean
- A value that is only True or False.
- Infinite loop
- A loop whose condition never becomes false.
The explanation · step 1 of 3 · part 1 of 2
Variables: labelled boxes
A variable is a named box in memory that holds a value.
score = 0 score = score + 10 print(score)
score = 0: make a box called score, put 0 in it. This is assignment.score = score + 10: read the right side first. 0 + 10 = 10. Put 10 back in the box.print(score)shows 10.
The explanation · step 1 of 3 · part 2 of 2
Variables: labelled boxes (continued)
In code, = means 'store', not 'equals'. To ask if two things are equal, use ==.
Scratch: the orange 'set score to 0' and 'change score by 10' blocks.
The explanation · step 2 of 3
Data types
| Type | Holds | Python examples |
|---|---|---|
Integer (int) | Whole numbers | 7, -3, 0 |
Float (float) | Decimal numbers | 3.14, 0.5 |
String (str) | Text in quotes | "Hello", "7" |
Boolean (bool) | True or False | True, False |
"7" in quotes is text. 7 + 7 gives 14, but "7" + "7" gives 77 (joining text, called concatenation).
The explanation · step 3 of 3
Arithmetic operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | Add | 7 + 2 | 9 |
| - | Subtract | 7 - 2 | 5 |
| * | Multiply | 7 * 2 | 14 |
| / | Divide | 7 / 2 | 3.5 |
| // | Whole-number divide (DIV) | 7 // 2 | 3 |
| % | Remainder (MOD) | 7 % 2 | 1 |
| ** | Power | 2 ** 8 | 256 |
n % 2 == 0 checks if a number is even.
The explanation · step 3 of 3
The maths: DIV and MOD
Year 8 · Python I · Lesson 2 of 5
Try it: Watch the variable boxes
Year 8 · Python I · Lesson 2 of 5
Quick quiz · 1 of 3
score = 45. Code: if score >= 25: print("Bronze") elif score >= 40: print("Gold"). What prints?
- A Bronze
- B Both
- C Gold
A. Bronze
45 >= 25 is True, so the first branch runs and the rest is skipped. The order is wrong: a logic error.
Year 8 · Python I · Lesson 2 of 5
Quick quiz · 2 of 3
A program runs with no message but says the average of 4 and 6 is 7. This is a…
- A Syntax error
- B Runtime error
- C Logic error
C. Logic error
It ran but gave the wrong answer: a logic error. (4 + 6 / 2 = 7; it needs brackets.)
Year 8 · Python I · Lesson 2 of 5
Quick quiz · 3 of 3
count = 5, then while count > 0: print(count). What happens?
- A Prints 5 for ever
- B Prints 5 4 3 2 1
- C Prints nothing
A. Prints 5 for ever
Nothing changes count, so count > 0 stays True: an infinite loop.
Year 8 · Python I · Lesson 2 of 5
Exit ticket
Answer on paper before you go. Then check.
What does print("3" + "4") show?
A 34B An errorC 7
A. 34 They're strings, so + joins them: 34.
What does 17 % 5 give?
A 3B 2C 3.4
B. 2 17 ÷ 5 = 3 remainder 2. % gives the remainder.