Data types
Integers, reals, booleans, characters and strings plus casting.
Learning Objectives
- 1Understand the five required data types: integer, real, Boolean, character and string
- 2Choose a suitable data type for data in a given scenario
- 3Understand that data types may be temporarily changed through casting, and where this is useful
What is a data type?
The five required data types
age = 25 # integer
price = 4.99 # real
is_open = True # Boolean
grade = 'A' # character
name = "Aleks" # stringage = 25 // integer
price = 4.99 // real
isOpen = True // Boolean
grade = 'A' // character
name = "Aleks" // stringChoosing a suitable data type
varchar is accepted as equivalent to string, and single/double as equivalent to real, since these are the terms used by common database software.Casting
age = int(input("Enter age: ")).age_text = input("Enter your age: ")
age = int(age_text)
print(age + 1)ageText = input("Enter your age: ")
age = int(ageText)
print(age + 1)Exam Phrasing: Use These Exact Phrases
- ✓An integer is a whole number; a real number includes a decimal/fractional part
- ✓A Boolean can only hold the value True or False
- ✓A string is a sequence of characters; a character is a single symbol or letter
- ✓Casting temporarily converts data from one data type to another
- 💡Always name the specific data type (integer, real, Boolean, character, string), "number" or "text" alone is not precise enough.
- 💡Remember that data entered by a user usually arrives as a string, even if it looks like a number, cast it before using it in a calculation.
- 💡When identifying casting in given code, look for functions like int(), float()/real(), str() or bool() being applied to a value.
- 💡In database questions, DBMS-specific terms like varchar (string) or single/double (real) are also accepted as valid data type answers.
Wrong:
Describing a field's data type as "number" or "input"
Right:
Naming the specific data type: integer, real, Boolean, character, or string
Why it matters: Examiners noted that vague answers like "number" or "input" do not demonstrate understanding of the specific data type required.
Wrong:
Treating "123" (a string) the same as 123 (an integer)
Right:
Recognising these are different data types that support different operations
Why it matters: A string of digits cannot be used directly in arithmetic without first being cast to a numeric type.
Wrong:
Assuming casting permanently changes how a value is stored everywhere
Right:
Casting produces a converted value, typically for immediate or temporary use
Why it matters: Casting converts data for use in a specific context, it does not retroactively change the original data's type everywhere it exists.
Question:
A program asks the user to enter their age, then checks if they are old enough to vote (18 or over). Explain why casting is needed in this algorithm, and identify where it occurs. [2]
age = int(input("Enter your age: "))
if age >= 18:
print("You can vote")
else:
print("Not old enough")age = int(input("Enter your age: "))
if age >= 18 then
print("You can vote")
else
print("Not old enough")
endif💡 Attempt this before revealing the answer, practice makes perfect!
Model Answer:
Casting is needed because input() always returns a string, even if the user types a number, and a string cannot be directly compared numerically with an integer like 18. Casting occurs on the line where the input is converted using int(), turning the string into an integer so it can be compared using >=.
Mark Scheme:
- 1Explains that input is received as a string and must be converted to compare it numerically
- 2Correctly identifies the int() casting operation as where this occurs
Tap a card to flip it. Use the arrow keys to move and space to flip.
Exam-style questions. Have a go, then reveal the mark scheme to check your answer.
1.Explain what is meant by "casting", giving an example of when it might be needed.
[2]2.A database stores information about books, including the number of pages and whether the book is currently on loan. State an appropriate data type for each of these two fields.
[2]3.Identify where casting occurs in the code below, and explain why it is necessary.
[2]total = 0
value = int(input("Enter a number: "))
total = total + value
print(total)total = 0
value = int(input("Enter a number: "))
total = total + value
print(total)4.A student says the value "42" (as a string) and the value 42 (as an integer) are the same. Explain why this is incorrect.
[2]5.State the most appropriate data type for each of the following: (a) a person's shoe size, e.g. 8 (b) whether a light is on or off (c) a postcode.
[3]6.Explain why a program that asks the user for their age must use casting, and describe what could go wrong if it did not.
[6]The examiners' reports are the most useful and least read of the three: they say what candidates actually got wrong each year. All files are hosted by OCR.
Past-paper questions grouped by topic
The same OCR questions, sorted by topic rather than by year (via Physics & Maths Tutor).
Casting was well understood by most candidates. When asked to identify how casting was used within a given algorithm, candidates needed to identify the specific value or variable being converted (e.g. to an integer), in context, a generic definition of casting alone was not sufficient for this type of question.
When asked to give the most appropriate data type for given database fields, most candidates who understood the concept of data types correctly identified string and real/float. Incorrect answers often gave a non-data-type response such as "variable" or "input", showing they did not understand what "data type" meant. DBMS-specific synonyms such as varchar (for string) and single/double (for real/float) were also accepted.