Skip to content

2.2.2 Data types

GCSE · Paper 2 · Computational thinking, algorithms and programming · Section 2.2 · 15 flashcards · 9 practice questions

Questions

2.2.2 Data types

Every value in a program has a data type. The type tells the computer what kind of data it is and what can be done with it. All code here is in the OCR Exam Reference Language.

Definition
A data type describes the kind of data a value or variable holds, such as a whole number, a decimal number or text.

The five data types

Data typeWhat it storesExamples
IntegerA whole number, positive, negative or zero7, -3, 0, 250
Real (also called float)A number with a decimal part3.14, 0.5, -2.75
BooleanOne of two values: True or FalseTrue, False
CharacterA single letter, digit or symbol"A", "7", "?"
StringA sequence of characters (text)"Hello", "Cust001", "07700 900123"

In the reference language, characters and strings are both written inside double quotes.

Key Point
A value in quotes is text, even if it looks like a number. 7 is an integer, but "7" is a string holding the character 7.

Why the type matters

The type decides what happens when you use an operator. Look at the difference.

pseudocode
print(7 + 1)         // 8
print("7" + "1")     // 71

With integers, + adds. With strings, + joins the text together. This is called concatenation. String handling is covered in 2.2.3.

The type of the result of a calculation also depends on the operator.

CalculationResultType of result
7 / 23.5Real
7 DIV 23Integer
7 MOD 21Integer
7 > 2TrueBoolean

Casting

Definition
Casting means changing a value from one data type to another.
FunctionWhat it doesExampleResult
str()Casts to a stringstr(345)"345"
int()Casts to an integerint("3")3
float() or real()Casts to a real numberfloat("4.52")4.52
bool()Casts to a Booleanbool("True")True

The reference language accepts both float() and real() for turning something into a real number. They do the same job.

Casting a real number with int() removes the part after the decimal point. It does not round.

Warning
A value can only be cast if it makes sense. int("3") works because the text is a whole number. int("hello") does not make sense, so the program would crash with an error.

Casting input

The input() command always gives you a string, even when the user types digits. To do a calculation you must cast the input first.

pseudocode
a = input("Enter first number")     // the user types 4
b = input("Enter second number")    // the user types 5
print(a + b)                        // 45
print(int(a) + int(b))              // 9

In the first print, a and b are strings, so + joins them to make "45". In the second print each one is cast to an integer, so + adds them to make 9.

Most programs cast straight away, in one line.

pseudocode
age = int(input("Enter your age"))
price = float(input("Enter the price"))

Casting to build a message

You cannot join a number onto a string with +. Cast the number to a string first.

pseudocode
score = 12
print("Score: " + str(score))       // Score: 12

Without str(), the program would try to join a string and an integer, which causes an error.

Worked Example
Task: The user types 5 when asked for the number of players and 2.5 when asked for the cost per player. Print the total cost.
pseudocode
players = int(input("Number of players"))
cost = float(input("Cost per player"))
total = players * cost
print("Total: " + str(total))       // Total: 12.5

players is cast to an integer and cost to a real number, so they can be multiplied. The answer 12.5 is a number, so it is cast with str() to be joined to the text.

Choosing a suitable data type

To choose a type, ask three questions.

  • 1.
    Is it a number or text?
  • 2.
    If it is a number, is it always whole, or can it have a decimal part?
  • 3.
    Will the program do calculations with it?
DataSuitable typeReason
Number of goals scoredIntegerA whole number that is counted
Price of a sandwich, 3.49RealHas a decimal part
Average test mark, 62.5RealAn average can have a decimal part
Whether a door is lockedBooleanOnly two possible values
First initial of a nameCharacterA single letter
SurnameStringText of any length
Phone number, 07700 900123StringSee below
Postcode, AB1 2CDStringContains letters and a space
Student ID, S2041StringContains a letter

Why a phone number is a string

Key Point
A phone number is stored as a string, not an integer. We never do calculations with a phone number, such as adding two of them. Also, an integer would lose the leading zero, so 07700 would become 7700. A string keeps every character, including zeros, spaces and a + sign.

The same idea applies to postcodes, account numbers and ID numbers. If the digits are a label rather than an amount, use a string.

Exam Tip
In a "choose the data type" question, name the type and give a reason that fits the data. For example: "Integer, because the number of tickets is a whole number." A type with no reason usually earns only one mark.

Data types in a program

Worked Example
Task: Store details about a customer and print a message.
pseudocode
firstInitial = "J"         // character
surname = "Okafor"         // string
age = 34                   // integer
balance = 12.5             // real
isMember = True            // Boolean
print(firstInitial + " " + surname + " is " + str(age))

The program prints J Okafor is 34. The integer age had to be cast with str() to join it to the strings. The tools for working on strings, such as slicing, are covered in 2.2.3.

Exam technique

  • •
    State or give: name the type, such as integer. One word is enough.
  • •
    Identify: look at the value. Quotes mean text. A decimal point means real. True or False means Boolean.
  • •
    Explain (why a type is suitable): link the type to the data, such as "a whole number" or "text that contains a letter" or "no calculations are done with it".
  • •
    State the result of casting: write the new value and its type. int("3") is the integer 3. str(3) is the string "3".
  • •
    Do not write "number" as a data type. Say whether it is an integer or a real.