Community resourceWorksheet
J277 2.2.2 Data types and casting
Part 2 of 7 · J277 2.2.1-2.2.2 · Programming fundamentals
The five data types OCR names, and converting between them when input arrives as text.
Students will:
- identify integer, real, Boolean, character and string data
- choose a suitable data type for a scenario and justify it
- explain why input arrives as a string
- use int() and float() when numerical input is required
- predict the result of an operation on mixed types
Inside: 6 explanation cells, 3 runnable Python tasks, 2 multiple-choice questions, 1 fill-in-the-blanks cell and 2 written answers. 21 marks, about 60 minutes.
Series: J277 2.2.1-2.2.2 · Programming fundamentals, part 2 of 7.
Shared by Coding PathwayVerified teacher
- 14 cells
- About 60 minutes
- CC BY-SA 4.0
- Shared 17 Aug 2026
- Updated 9 Sept 2026
Preview
The whole resource, exactly as a class sees it. Answers and marking are held back.
Data types and casting
Programs store different kinds of data in different ways. Choosing a suitable type matters because it controls the values that can be represented and the operations that make sense. A whole-number value, such as a lap count or number of goals, uses the integer data type. A name needs a text type instead.
OCR expects you to recognise integer, real, Boolean, character and string data, choose suitable types for a scenario, and convert between types when necessary. In Python, float represents real-number values and a one-character string represents character data.
By the end, you should be able to identify a value's type, justify a suitable type and use int() or float() when numerical input is required.
Which data type is most suitable for the number of goals scored in a match?
- ABoolean
- BString
- CInteger
- DReal
Choosing a type
- Integer: a positive or negative whole number, or zero, such as
12,0or-4. - Real: a number that may have a fractional part, represented by
floatin Python, such as12.5. - Boolean: one of two logical values,
TrueorFalse, such as whether a door is locked. - Character: one symbol, such as
"A","7"or"?". Python represents this as a string with a length of one. - String: a sequence of characters, such as
"Aisha"or"AB12 3CD".
Choose a type from the meaning of the data, not just its appearance. A postcode may contain digits, but it is a string because it is an identifier and is not used in arithmetic.
False is label 1. The value 27 is an label 2. The value 27.5 is a label 3. The value "Q" is a label 4. The value "OCR" is a label 5.- character
- string
- Boolean
- real
- integer
- constant
Casting: changing a value's type
Python's input() function returns a string, even when the user types digits. The value "12" is text, so it must be cast before it can be used as the whole number 12 in arithmetic.
int("12")produces the integer12.float("12.5")produces the real value12.5.str(12)produces the string"12".
Casting is useful when the type you have is different from the type an operation needs. It does not automatically change the original variable: after age = int(age_text), age_text still stores a string and age stores an integer.
Choose the cast that matches the valid data. A quantity is normally an integer; a price or measured distance may need a real value.
Before running the example below, predict the type of price, the type of quantity and the value that will be displayed. Then run it to check your reasoning.
price_text = "4.50"
quantity_text = "3"
# Convert the stored text before doing arithmetic.
price = float(price_text)
quantity = int(quantity_text)
total = price * quantity
print(total)
Why does age = int(input("Age: ")) use int()?
- ATo display the age
- BTo make the value Boolean
- CTo create a constant
- DTo convert input text to a whole number
Supported practice: total distance
A running app currently stores two values as text:
distance_textstores the distance of one lap, which may contain a fractional part.laps_textstores the number of completed laps, which must be a whole number.
Complete the program below so that it:
- casts
distance_textto a real value and stores it indistance; - casts
laps_textto an integer and stores it inlaps; - calculates
distance * lapsand stores the answer intotal_distance; - displays the total distance.
For the supplied values, the program should display 50.0. The checks will also use another pair of valid values, so calculate from the variables rather than typing the final answer directly.
distance_text = "12.5"
laps_text = "4"
# 1. Cast distance_text to a real value.
# 2. Cast laps_text to an integer.
# 3. Calculate the total distance.
# 4. Display the result.
# Write your statements below.
Choose a suitable data type for each value and justify each choice: a user's email address, whether a door is locked, and the temperature measured by a weather station.
Name the type and link it to the values that must be represented.
Students type their answer here.
Independent practice: order total
A small shop needs a program that calculates the total cost of an order. The user will enter:
- a price, which may include pence and therefore needs a real value;
- a quantity, which is a whole number and therefore needs an integer.
Write a complete program that inputs and casts both values, calculates price * quantity, stores the result in total, and displays it.
Use the variable names price, quantity and total. You may cast each input on the same line that collects it, or store the input text first and cast it afterwards. The checks will run the program with more than one order, so the result must be calculated from the inputs.
When testing it yourself, try a price of 4.50 and a quantity of 3. The calculated total should be 13.5. A short label in the displayed message is acceptable.
# 1. Input and cast price as a real value.
# 2. Input and cast quantity as an integer.
# 3. Calculate price multiplied by quantity and store it in total.
# 4. Display total.
A student uses an integer for a runner's finishing time of 12.47 seconds. Explain the problem and give a better type.
State what information would be lost and name the Python type used.
Students type their answer here.
Review
You should now be able to:
- distinguish integer, real, Boolean, character and string values;
- choose a suitable type by considering the values and operations required;
- remember that
input()returns a string; - cast input with
int()orfloat()before numerical processing; - explain why an unsuitable type can lose information or prevent the intended operation.
In an exam question that asks you to justify a data type, name the type and link it to the data. For example: “Real, because a finishing time may contain a fractional part.”