2.2.2

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?

A data type tells the computer what kind of data a variable is holding, a whole number, some text, or a true/false value, for example. This affects how the data is stored and what you can do with it.
A data type defines the kind of data a variable or constant can hold, which affects how it is stored, processed and displayed, and which operations can validly be performed on it. Choosing the correct data type matters: some operations only make sense for certain types, for example you cannot meaningfully divide two pieces of text.
Data types constrain both storage and behaviour: the computer allocates storage appropriately for the type, and only permits operations that are valid for it. The value 123 (an integer) and the value "123" (a string) are stored and treated completely differently, even though they display the same digits, this distinction is a common source of confusion and a common exam question.

The five required data types

You need to know five data types: integer (a whole number), real (a number with a decimal point), Boolean (True or False), character (a single letter/symbol), and string (a sequence of characters, i.e. text).
The five data types required are: integer (whole numbers, no decimal point, e.g. 5, -20), real (numbers with a fractional/decimal part, e.g. 3.14, sometimes called "float"), Boolean (only True or False), character (a single symbol or letter, e.g. 'A'), and string (a sequence of characters, i.e. any piece of text, e.g. "Hello").
Precision matters when naming a data type in an exam answer, "number" is not a valid answer, you must specify integer or real. Similarly, "text" alone is imprecise if the exact required term is string. Real and float are the same data type and mark schemes generally accept either, but real is the term the specification uses, so it is the safer one to write. In practice, a single character is really just a string of length one, but the specification requires you to know both terms separately.
The five data typesInteger25A whole numberReal4.99Has a decimalpointBooleanTrueOnly Trueor FalseCharacter'A'One singlesymbolString"Aleks"A sequence ofcharactersCasting"42"Stringint()42IntegerCasting converts a value from onetype to another, so typed input canbe used in a calculation, not joinedon as text.
Python· Declaring each of the five data types
age = 25          # integer
price = 4.99      # real
is_open = True    # Boolean
grade = 'A'       # character
name = "Aleks"     # string
OCR ERL· Declaring each of the five data types
age = 25          // integer
price = 4.99      // real
isOpen = True     // Boolean
grade = 'A'       // character
name = "Aleks"     // string

Choosing a suitable data type

When designing a program, you need to pick the right data type for each piece of data. A person's age should be an integer, their height in metres should be a real, and whether they've paid should be a Boolean.
Choosing a suitable data type for a given scenario is a required, examined skill. Ask what the data actually represents: a whole count needs an integer, a measurement or price usually needs a real, a yes/no condition needs a Boolean, and any free text (like a name or an address) needs a string. Choosing the wrong type can cause errors or waste storage.
When justifying a data type choice, name the specific data type (integer, real, Boolean, character, or string), not a vague description like "number" or "text". In database contexts, DBMS-specific synonyms are also accepted, for example 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

Casting means temporarily converting data from one type to another. For example, input from a user usually arrives as a string, even if they typed a number, so you often need to cast it to an integer before doing maths with it.
Casting is the process of changing a piece of data from one data type to another, usually so it can be used with an operation that requires a different type. A very common example is casting user input (which is normally received as a string) to an integer or real, so it can be used in a calculation, e.g. age = int(input("Enter age: ")).
Casting is temporary, it does not permanently change how the original data is stored, it produces a converted value (often reassigned into the same or a new variable). Exam questions often ask you to identify where casting occurs in a given algorithm, look for a function like int(), float()/real(), or str() being applied to a variable, this is generally well understood by candidates when they read the code carefully.
Python· Casting user input to an integer before using it in a calculation
age_text = input("Enter your age: ")
age = int(age_text)
print(age + 1)
OCR ERL· Casting user input to an integer before using it in a calculation
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]

Python· Where does casting occur?
age = int(input("Enter your age: "))
if age >= 18:
    print("You can vote")
else:
    print("Not old enough")
OCR ERL· Where does casting occur?
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.

Known 0 Still learning 0
1 / 12

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]
Python· Identify the casting
total = 0
value = int(input("Enter a number: "))
total = total + value
print(total)
OCR ERL· Identify the casting
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).

2025
Paper 2

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.

Paper 2

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.