2.2.2 Data types · Paper 2
Data types and casting
Each section has pictures and things to try, then its key points and key words. Exam practice is at the end of the page.
You need to know
- Integer, real, Boolean, character and string data types
- Choosing a suitable data type for data in a scenario
- Casting: changing a value's type, and when it's useful
You don't need to know
- OCR doesn't rule anything out here.
Data types
| Type | Holds | Example |
|---|---|---|
| Integer | Whole numbers | 42, -7 |
| Real (float) | Numbers with a decimal part | 3.14, 9.99 |
| Boolean | True or False only | True |
| Character | A single letter, digit or symbol | "A" |
| String | A sequence of characters | "Hello" |
Watch out: Phone numbers are stored as strings, not integers. They can start with 0 and you never do maths with them.
Try it
Which data type would you use?
- The number of students in a class
- The price of a drink, £1.25
- Whether a door is locked
- A grade, like A or B
- A phone number, 07700 900123
- A temperature, 21.5°C
- A postcode, like SW1A 1AA
Key points
Data types
Before this: 2.2.1Variables and constants
- Integer: whole numbers, e.g. 42.
- Real (or float): numbers with a decimal part, e.g. 3.75.
- Boolean: only True or False.
- Character: one letter, digit or symbol, e.g. "A".
- String: a sequence of characters, e.g. "Hello".
- Phone numbers are strings: they can start with 0 and you never do maths with them.
Key words
Say what each one means, then tap to check.
Casting
Casting changes a value from one type to another. input() always gives a string, so you cast it to do maths.
age = int(input("Age? ")) # string to integer print("You are " + str(age)) # integer to string to join text
- int() makes an integer, float() (OCR: real()) makes a real, str() makes a string and bool() makes a Boolean.
- Casting fails if the value doesn't fit, e.g. int("hello") causes an error.
Try it
Type something, as if a user typed it. What does each cast do?
Try it
Predict what each line prints. Then tap to check.
Key points
Casting
Before this: Data types2.2.1Input and output
- Casting means changing a value from one data type to another.
- int("42") 42, float("3.5") (or real) 3.5, str(42) "42".
- input() always gives a string, so cast it before doing maths.
- Cast numbers to strings before joining them to text with +.
Key words
Say what each one means, then tap to check.
Exam practice
7 questions in the style of the OCR exam, 17 marks in total. Try each one, then check the mark scheme.
Data types
The number of students in a class
A person's height in metres
Whether a customer is a member
A postcode
The first letter of a surname
Hint
For each one, ask: is it a whole number, a decimal, True or False, one character, or text?
Give means give a short, clear answer. You don't need to explain it.
5 marks: make 5 separate points.
Show mark scheme
- Integer
- Real / float
- Boolean
- String
- Character
Also accept: String for the first letter of a surname.
Don't accept: Integer for the postcode. It contains letters and a space.
1 mark for each.
Hint
What would happen to the 0 at the start of 07700 900123 if it was stored as a number?
Explain means say what happens and why. Words like “because” and “so” help.
1 mark: one point, with the reason.
Show mark scheme
- It can start with 0 (which an integer would lose) / it may contain spaces or + / no maths is done with it.
Casting
age = input("Age") print(age + 1)
Hint
What type of data does input() give? Can you add 1 to a string?
Explain means say what happens and why. Words like “because” and “so” help.
2 marks: aim for 2 things said, such as a point and then the reason for it.
Show mark scheme
- age is a string, so you can't add a number to it.
- Cast it: age = int(input("Age")).
1 mark for the reason, 1 mark for the fix.
print(str(5) + str(5))
print(int("5") + int("5"))
Hint
Try it in the casting table. What does + do to two strings? What does it do to two integers?
State means give a short, clear answer. You don't need to explain it.
2 marks: make 2 separate points.
Show mark scheme
- 55
- 10
Don't accept: 10 for the first line. Two strings are joined, not added.
1 mark for each.
Mixed questions on the whole page
Hint
Whole number or decimal? Then work out 3 × 2.5. Does the answer have a decimal part?
State means give a short, clear answer. You don't need to explain it.
3 marks: make 3 separate points.
Show mark scheme
- quantity: integer
- price: real / float
- quantity * price: real / float (it is 7.5)
Also accept: price: "float" or "decimal".
Don't accept: quantity * price: "integer". 3 × 2.5 = 7.5, which is not a whole number.
1 mark for each.
Hint
What does casting change? What type does input() always give?
Explain means say what happens and why. Words like “because” and “so” help.
2 marks: aim for 2 things said, such as a point and then the reason for it.
Show mark scheme
- Casting changes a value from one data type to another.
- Input is stored as a string, so it must be cast to an integer / real to do calculations.
1 mark for each.
Hint
You can't join an integer to a string with +. What do you need to do to points first?
Write means write the code or algorithm. OCR Exam Reference Language or Python both get marks.
2 marks: each part that works earns marks, even if the whole thing isn't perfect.
Show mark scheme
- Casts points to a string with str(points)…
- …and joins it to the text with +, e.g. print("You scored " + str(points) + " points")
Also accept: print("You scored", points, "points") in Python, which needs no cast.
1 mark for each point.