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.

  1. Last lesson

    How many times does for i in range(2, 10, 2): run?

    A 4B 5C 8

    A. 4

  2. Last unit

    What is 1011 in denary?

    A 13B 11C 7

    B. 11

  3. 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)
  1. score = 0: make a box called score, put 0 in it. This is assignment.
  2. score = score + 10: read the right side first. 0 + 10 = 10. Put 10 back in the box.
  3. print(score) shows 10.

The explanation · step 1 of 3 · part 2 of 2

Variables: labelled boxes (continued)

!Watch out

In code, = means 'store', not 'equals'. To ask if two things are equal, use ==.

e.g.Example

Scratch: the orange 'set score to 0' and 'change score by 10' blocks.

The explanation · step 2 of 3

Data types

TypeHoldsPython examples
Integer (int)Whole numbers7, -3, 0
Float (float)Decimal numbers3.14, 0.5
String (str)Text in quotes"Hello", "7"
Boolean (bool)True or FalseTrue, False
!Watch out

"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

OperatorMeaningExampleResult
+Add7 + 29
-Subtract7 - 25
*Multiply7 * 214
/Divide7 / 23.5
//Whole-number divide (DIV)7 // 23
%Remainder (MOD)7 % 21
**Power2 ** 8256
e.g.Example

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?

  1. A Bronze
  2. B Both
  3. 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…

  1. A Syntax error
  2. B Runtime error
  3. 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?

  1. A Prints 5 for ever
  2. B Prints 5 4 3 2 1
  3. 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.

  1. What does print("3" + "4") show?

    A 34B An errorC 7

    A. 34 They're strings, so + joins them: 34.

  2. What does 17 % 5 give?

    A 3B 2C 3.4

    B. 2 17 ÷ 5 = 3 remainder 2. % gives the remainder.