OCR GCSE COMPUTER SCIENCE · J277 · PAPER 2
Variables and data types
Labelled boxes for data. What goes in, what type it is, and how to change it.
Paper 2 · Lesson 22
Lesson by Computing
This lesson contains 21 slides, with interactive quizzes and text slides.
OCR GCSE COMPUTER SCIENCE · J277 · PAPER 2
Variables and data types
Labelled boxes for data. What goes in, what type it is, and how to change it.
Paper 2 · Lesson 22
Lesson 22 of the J277 series (Block D, Paper 2, sections 2.2.1 and 2.2.2 Programming fundamentals and data types). The first lesson of Block D.
Cover photo: Cabri Caldwell / Unsplash (hologram of labelled storage jars).
Device-free: show the slide; pupils write the title and date. Image credit: Cabri Caldwell / Unsplash (cover hologram)
00 · MISSION · BY THE END OF THIS LESSON
By the end of this lesson you can…
01 Explain the difference between a variable and a constant.
02 Choose the right data type for a value, and cast between types.
03 Use arithmetic, comparison and Boolean operators, including MOD and DIV.
Read the three outcomes. J277 section 2.2 expects pupils to use variables, constants, inputs, outputs and assignment; to choose between integer, real, Boolean, character and string; to cast between types; and to use arithmetic (including MOD, DIV and exponent), comparison and Boolean operators.
Device-free: pupils copy the outcomes and tick each one at the end.
02 · CHECK · DO NOW (LESSON 21)
Warm up: languages and IDEs
1 Give one difference between high-level and low-level languages.
2 Which translator makes an executable file?
3 Name one IDE tool that helps you find errors.
4 What does a translator do?
▮ TERMINAL // do_now_answers
1 high-level is close to English;
low-level is close to the CPU
2 a compiler
3 e.g. error diagnostics,
breakpoints, stepping
4 turns source code into
machine code
Retrieval from lesson 21. Give 4 minutes, then click to reveal the answers line by line.
1 High-level languages are close to English, easy to read and portable; low-level languages (machine code and assembly) are close to the hardware and written for one type of CPU. Accept any valid difference. 2 A compiler: it translates the whole program at once and produces an executable. 3 Any one of: error diagnostics, breakpoints, stepping through the code one line at a time, watching variables. 4 It translates source code (high-level or assembly) into machine code the CPU can execute.
Device-free: questions on the board; mini whiteboards.
Recap. Python is an example of which kind of language?
Machine code
Assembly code
High-level
Low-level
Answer: C, high-level. Python is close to English, portable, and must be translated (Python is usually interpreted) before the CPU can run it.
Machine code and assembly code are both low-level, so D is the general term for A and B. All the code in this lesson is high-level: OCR Exam Reference Language and Python.
Photo: Luca Bravo / Unsplash.
Device-free: students hold up A, B, C or D cards. Image credit: Luca Bravo / Unsplash
01 · LEARN · MISSION BRIEFING
Store and shape data
Every program stores data while it runs. Today: where it goes, what type it is, and what you can do with it.
VARIABLES
Named places in memory that hold a value.
DATA TYPES
Whole numbers, decimals, True or False, text.
OPERATORS
Do maths on values and compare them.
SYSTEM VOICE
System online. Today's mission: store and shape data. Learn how variables and constants hold values, which data type each value needs, how to cast a value from one type to another, and how operators work on them.
Click play for the system voice briefing. The words are also on the slide.
Quick hook: ask what a game has to remember while it runs (score, lives, player name, whether the game is paused). Each of those is a variable, and each needs a different data type.
Device-free: pupils list three things a game must remember and guess what kind of value each one is.
01 · LEARN · VARIABLES AND CONSTANTS
A name for a value
VARIABLE its value can change
CONSTANT its value stays fixed
= assigns a value to a name
input() reads, print() outputs
▮ TERMINAL // variables.erl
1 const VAT = 0.2
2 name = input("Name? ")
3 price = 50
4 price = price + 10
5 print(price * VAT)
SYSTEM VOICE · KEY DEFINITION
A variable is a named location in memory that holds a value. The value can change while the program runs. A constant also has a name, but its value cannot change while the program runs.
Click to reveal the four ideas, then play the key definition.
Walk through the terminal (OCR Exam Reference Language). Line 1 declares a constant: VAT never changes while the program runs. Line 2 is an input: whatever the user types is stored in name. Lines 3 and 4 are assignments: price starts at 50, then the right-hand side is worked out first (50 + 10) and the result, 60, replaces the old value. Line 5 is an output: 60 * 0.2 = 12, so the program displays 12 (Python shows 12.0).
Why use a constant? The value is set in one place, so if VAT changes it is updated once, and the program cannot change it by mistake. Python has no true constants: programmers write the name in capitals, VAT = 0.2, to show it should not change.
Misconception: = in programming does not mean "equals" as in maths. It means "store the value on the right in the name on the left".
Device-free: pupils trace the five lines and write the value of price after each line.
01 · LEARN · DATA TYPES
Every value has a type
INTEGER whole number: 42, -7
REAL has a decimal part: 3.75
BOOLEAN only True or False
CHARACTER one symbol: 'A', '?'
STRING text: "Hello"
▮ TERMINAL // types.py
age = 15 # integer
height = 1.62 # real
paid = True # Boolean
initial = "S" # character
name = "Sam" # string
Watch out: a phone number such as "07700 900123" is a string. It starts with 0, has a space, and you never do maths on it.
Click to reveal the five data types J277 names. Integer: a whole number, positive or negative. Real: a number with a decimal part (Python calls it float). Boolean: only True or False. Character: a single letter, digit or symbol. String: a sequence of characters, written in quotes.
Python has no separate character type: a character is just a string of length 1, so initial = "S" is a string in Python. In the exam, a single character should still be called a character.
The phone number: an integer would lose the leading 0 (07700 would become 7700), cannot store the space, and adding or multiplying phone numbers makes no sense. The same goes for house numbers like 12A and postcodes.
Device-free: pupils write one example of their own for each of the five types.
01 · LEARN · THE BIG IDEA
A variable is a labelled jar
1 The label is the name (identifier).
2 What is inside is the value.
3 Empty it and refill it: assignment replaces the old value.
4 Each jar holds one kind of thing: its data type.
5 A sealed jar is a constant.
Remember: the name stays the same, the contents can change.
Click to reveal the points. The picture is the idea to remember: each jar has a label (the variable's name), something inside (its value) and holds one kind of thing (its data type). You find the jar by its label, not by what is in it, just as a program finds a value by its variable name.
Assignment empties the jar and puts something new in: the old value is gone. A constant is a jar that is sealed once it is filled, so the contents cannot change while the program runs.
Photo: Luba Ertel / Unsplash.
Device-free: pupils draw three labelled jars for score, playerName and gameOver, write a value in each and the data type underneath. Image credit: Luba Ertel / Unsplash
01 · LEARN · CASTING
input() always gives a string
▮ TERMINAL // bug.py
1 age = input("Age? ")
2 print(age + 1)
> Age? 15
> TypeError: can only concatenate str (not "int") to str
▮ TERMINAL // fixed.py
1 age = int(input("Age? "))
2 print(age + 1)
> Age? 15
> 16_
int("15")
gives the integer 15
float("3.5")
gives the real 3.5 (ERL: real())
str(16)
gives the string "16"
Casting means changing a value from one data type to another.
The classic bug (left, Python): input() always returns a string, even when the user types digits. So age holds the string "15", and "15" + 1 tries to join a string and an integer. Python stops with the error shown: TypeError: can only concatenate str (not "int") to str.
The fix (right, click to run it): int() casts the string "15" to the integer 15 before it is stored, so age + 1 is 16. In OCR Exam Reference Language this is age = int(input("Age? ")), exactly the same.
The three casting functions: int() to an integer, float() to a real (OCR ERL also accepts real()), str() to a string. str() is needed to join a number into a message, for example print("Next year: " + str(age + 1)) displays Next year: 16. Ask: what does "3" + "4" give? ("34": both are strings, so + joins them.)
Device-free: pupils copy the bug, circle the problem and write the fixed line.
01 · LEARN · ARITHMETIC OPERATORS
Doing maths with MOD and DIV
+ − * add, subtract, multiply
/ divide: 7 / 2 = 3.5
^ exponent (power): 2 ^ 3 = 8
MOD the remainder after dividing
DIV whole-number division, remainder thrown away
▮ TERMINAL // operators (.erl vs .py)
ERL Python gives
17 MOD 5 17 % 5 2
17 DIV 5 17 // 5 3
2 ^ 3 2 ** 3 8
7 / 2 7 / 2 3.5
17 ÷ 5 = 3 remainder 2. DIV gives the 3. MOD gives the 2.
Click to reveal each worked example. Do each one on the board first.
17 MOD 5: 5 goes into 17 three times (15), remainder 2, so 17 MOD 5 = 2. In Python: 17 % 5 gives 2. 17 DIV 5: the whole number of times 5 goes into 17, so 17 DIV 5 = 3. In Python: 17 // 5 gives 3. 2 ^ 3: 2 to the power 3 = 2 × 2 × 2 = 8 (not 6). In Python: 2 3 gives 8. Careful: ^ means something else in Python, so always use there. 7 / 2: ordinary division gives 3.5, a real. In Python, / always gives a real, so even 6 / 2 gives 3.0.
Uses: MOD 2 tells you if a number is even (n MOD 2 = 0). DIV and MOD together split 135 minutes into 135 DIV 60 = 2 hours and 135 MOD 60 = 15 minutes.
Device-free: pupils work out 20 MOD 6 (2), 20 DIV 6 (3) and 3 ^ 2 (9) on mini whiteboards.
01 · LEARN · COMPARISON AND BOOLEAN OPERATORS
True or False?
== equal to != not equal to
< less than <= less than or equal to
> more than >= more than or equal to
AND True if both sides are True
OR True if at least one side is True
NOT flips True and False
▮ TERMINAL // compare.erl
age = 15
age >= 16 → False
age == 15 → True
age > 11 AND age < 16 → True
age < 11 OR age > 16 → False
NOT(age == 15) → False
Do not mix them up: = assigns a value, == compares two values.
Comparison operators compare two values and always give a Boolean: True or False. Boolean operators join or flip Boolean values.
Click through the trace with age = 15. age >= 16: 15 is not 16 or more, False. age == 15: True. age > 11 AND age < 16: both sides are True, so True. age < 11 OR age > 16: both sides are False, so False. NOT(age == 15): age == 15 is True, NOT flips it, False.
In Python the comparison operators are the same; the Boolean operators are written in lower case: and, or, not. Python displays the results as True and False.
Next lesson uses these inside if statements and loops.
Device-free: pupils work out each line for age = 11 instead (False, False, False, False, True).
Which data type is each value?
INTEGER
REAL
BOOLEAN
STRING
9.0
"07700 900123"
42
True
"42"
False
3.75
-15
Answers. Integer: 42 and -15 (whole numbers can be negative). Real: 3.75 and 9.0 (the .0 makes it a real, even though it is a whole amount). Boolean: True and False. String: "42" and "07700 900123" (anything in quotation marks is a string, even if it looks like a number).
The character type is left out here to keep four columns; a single symbol such as 'A' would be a character.
Device-free: pupils draw four columns and write each value in the right one.
17 eggs are packed in boxes of 6. What is 17 MOD 6?
2
5
3
11
Answer: B, 5. Two full boxes hold 12 eggs, and 17 − 12 = 5 eggs are left over. MOD gives the remainder, so 17 MOD 6 = 5. In Python: 17 % 6 gives 5.
Wrong answer 2 is 17 DIV 6, the number of full boxes. 3 is a guess from 17 ÷ 6 ≈ 2.8 rounded up. 11 is 17 − 6, one box taken away.
Photo: Denny Sachtleben / Unsplash.
Device-free: students hold up A, B, C or D cards. Image credit: Denny Sachtleben / Unsplash
x = input("Age? ") and the user types 15. What data type is x?
Integer
Real
Boolean
String
Answer: D, string. input() always returns a string, whatever the user types, so x holds "15". To use it as a number it must be cast first: x = int(input("Age? ")).
Integer is the common wrong answer, because 15 looks like a whole number. This is exactly the bug from the casting slide.
Photo: Alicia Christin Gerald / Unsplash.
Device-free: students hold up A, B, C or D cards. Image credit: Alicia Christin Gerald / Unsplash
03 · DEBUG · SPOT THE MISTAKE
One comment in Amara's code is wrong
▮ TERMINAL // amara_operators.py
1 print(17 // 5) # shows 3
2 print(17 % 5) # shows 2
3 print(2 ** 3) # shows 6
4 print(7 / 2) # shows 3.5
5 print("3" + "4") # shows 34
Line 3. 2 ** 3 means 2 to the power 3: 2 × 2 × 2 = 8, not 2 × 3 = 6.
Run each line in your head. Then tap the marker.
Tip: line 5 is right. "3" and "4" are strings, so + joins them.
Give a minute to find the mistake before tapping the marker. The comments (after #) are Amara's notes on what each line displays.
Answer: line 3. 2 ** 3 is the exponent: 2 × 2 × 2 = 8. Writing 6 is the common slip of multiplying 2 by 3. The other lines are correct: 17 // 5 is 3, 17 % 5 is 2, 7 / 2 is 3.5, and "3" + "4" joins two strings to make 34.
Device-free: copy the five lines onto the board; pupils write the wrong line number and the correct output.
int() casts the string to an integer
str() casts the result to output it
input() returns them as a string
The user types some digits
The program does the calculation
The program displays a prompt
A program asks for a number and does maths with it. Put the steps in order.
Correct order: 1 the program displays a prompt; 2 the user types some digits; 3 input() returns them as a string; 4 int() casts the string to an integer; 5 the program does the calculation; 6 str() casts the result so it can be joined into an output message.
In code: age = int(input("Age? ")) covers steps 1 to 4; age = age + 1 is step 5; print("Next year: " + str(age)) is step 6.
Device-free: pupils write the six steps on sticky notes and order them on the desk.
Complete the summary.
A variable is a named memory location whose value can change. A constant keeps the same value while the program runs. input() always returns a string, so use int() to turn it into a whole number. 17 MOD 5 gives 2 and 17 DIV 5 gives 3.
Answers in order: variable, constant, string, int, 2, 3. Distractors: Boolean, float, 4.
float() would give a real, not a whole number, so int is the only right cast here.
Device-free: paragraph on the board with gaps and the word bank; pupils write the six missing words.
EXAM · A program stores a pupil's name, age, height in metres and whether they have paid. Give the most suitable data type for each. [4 marks]
Exam-style question, 4 marks. Give 4 minutes. The model answer is on the next slide.
Mark scheme: 1 mark each. Name: string. Age: integer. Height in metres: real (accept float). Paid: Boolean.
Do not accept vague answers such as "number" or "text": name the data type. Do not accept integer for height (1.62 m has a decimal part) or string for paid.
Device-free: pupils answer on paper, then peer-mark with the next slide.
04 · EXAM · MODEL ANSWER
One type for each value
▮ TERMINAL // pupil_record
name → string
e.g. "Leah Okafor"
age → integer
e.g. 15
height → real
e.g. 1.62
hasPaid → Boolean
e.g. True
MARK SCHEME
1 mark: name, string
1 mark: age, integer
1 mark: height, real (or float)
1 mark: paid, Boolean
Name the type. "Number" or "text" scores 0.
Click to reveal the model answer one value at a time.
Name: string, because it is text. Age: integer, because ages are counted in whole years. Height in metres: real, because it has a decimal part (1.62). Paid: Boolean, because it is only ever yes or no, True or False.
Common mistakes: integer for height, string for paid ("yes" works, but Boolean is the most suitable), and vague answers like "number".
Device-free: pupils check their own answer against the four marks and correct it in a different colour.
How sure are you about data types?
Very: I can choose and cast types
Fairly: MOD and DIV are tricky
Not yet: I need another go
Anonymous exit poll. If many pupils find MOD and DIV tricky, start lesson 23 with three quick MOD/DIV calculations, since selection often uses MOD (for example checking if a number is even).
Device-free: thumbs up, sideways or down.
05 · NEXT · KEEP GOING
Keep going
Isaac Computer Science
Free online lessons and questions. Look for the topics on variables and data types.
BBC Bitesize
GCSE Computer Science, OCR: revise programming fundamentals and data types.
Try it in Python
Ask for an age, cast it with int(), and print the age in 10 years using str().
Next lesson: selection and iteration, if statements and loops.
Point pupils to the places to practise. Isaac Computer Science and BBC Bitesize are free.
The Python challenge, as a model: age = int(input("Age? ")) then print("In 10 years you will be " + str(age + 10)). Typing 15 displays: In 10 years you will be 25.
Device-free: pupils write the two-line program on paper and trace it for an age of 15, to bring to lesson 23.