- What a data type is and why programs need data types.
- How to use integer, real, Boolean, character and string data.
- How to choose a suitable data type for a given scenario.
- How casting can temporarily change a value’s data type.
A program stores and processes values. A value is a single piece of data, such as 42, "Alex", True or 3.75.
A variable is a named place in memory that stores a value while a program runs. For example, score = 10 stores the value 10 in a variable called score.
Data type
A data type describes what kind of value is being stored and what operations can sensibly be performed on it.
For example, 10 + 5 should do number addition, but "10" + "5" may join text together to make "105" in many high-level languages.
A high-level language is a programming language designed to be easier for humans to read and write than machine code. Python is a common classroom example, but OCR may also accept clear pseudocode or other high-level languages.
Types control meaning
The same-looking data can behave differently depending on its type. 7 as an integer is a number; "7" as text is a character or string.
An integer is a whole number with no decimal point. It can be positive, negative or zero.
Examples:
In Python, you might write:
| Purpose | Example |
|---|
| Player score | score = 0 |
| Number of attempts | attempts = 3 |
| Temperature rounded to whole degrees | temperature = -2 |
Integers are suitable when the value is counted in whole units, such as lives in a game, number of students, stock quantity or goals scored.
A real number can include a decimal part. In Python, this is usually stored using a type called float, but the GCSE term you should know is real.
Examples:
Use a real data type when fractional values are possible, such as distance, height, mass, time or a measurement from a sensor.
Using integer for measurements
If a value might need a decimal part, do not choose integer. For example, a person’s height could be 1.72, so real is more suitable than integer.
A Boolean value can only be one of two values: True or False.
Booleans are used for decisions. They often store whether something is currently true in a program.
Examples:
| Purpose | Boolean variable |
|---|
| User has logged in | isLoggedIn = True |
| Game has ended | gameOver = False |
| Password is correct | passwordCorrect = True |
A condition is an expression that evaluates to a Boolean value. For example:
age >= 18
score == 0
username != ""
You can combine Boolean values using Boolean operators:
AND means both parts must be true.
OR means at least one part must be true.
NOT reverses the truth value.
Boolean variable names
Boolean variables are often clearer if they sound like yes/no questions, such as hasTicket, isMember or gameOver.
Evaluating a Boolean condition
Suppose a cinema program uses this condition:
canEnter = age >= 13 OR hasAdult == True
For age = 11 and hasAdult = True:
- Evaluate the first comparison:
age >= 13 becomes 11 >= 13, which is False.
- Evaluate the second comparison:
hasAdult == True becomes True == True, which is True.
- Apply
OR: False OR True gives True, so canEnter is True.
A character is a single symbol, such as:
"A"
"7"
"?"
" " for a space character
Characters can be letters, digits, punctuation marks or other symbols.
In some languages, characters are written using single quotes, such as 'A'. In Python, there is no separate character type: a single character is stored as a string of length 1.
A string is a sequence of characters. It is used for text.
Examples:
"Hello"
"Alex"
"A123"
"07 1234 5678"
"" for an empty string
A string can contain letters, digits, spaces and punctuation. Digits inside a string are not treated as numbers for arithmetic.
A concatenation is when strings are joined together. For example, "Comp" + "uter" makes "Computer".
Treating all digits as numbers
Phone numbers, postcodes and usernames are usually strings, not integers. You do not do arithmetic with them, and they may contain spaces, letters, symbols or leading zeroes.
In exam questions, you may be given a scenario and asked to choose data types for different pieces of data.
Use these questions:
| Question | Likely data type |
|---|
| Is it a whole number used for counting? | Integer |
| Can it have a decimal part? | Real |
| Is it only true or false? | Boolean |
| Is it one symbol? | Character |
| Is it text, an ID, a phone number or a code? | String |
Choosing data types for a user profile
A program stores details about a user: age, height in metres, whether they accept emails, middle initial and mobile number.
age is a count of completed years and should not have a decimal part, so integer is suitable.
heightMetres might be 1.68 or 1.75, so real is suitable.
acceptsEmails has only two possible states, True or False, so Boolean is suitable.
middleInitial is one symbol, such as "K", so character is suitable.
mobileNumber may contain a leading 0, spaces or +44, and it is not used for arithmetic, so string is suitable.
Here are the GCSE data types shown using Python-style examples:
| GCSE data type | Python-style example | Notes |
|---|
| Integer | lives = 3 | Whole number |
| Real | distance = 4.5 | Number with a decimal part |
| Boolean | isOpen = True | Either True or False |
| Character | grade = "A" | Python stores this as a one-character string |
| String | name = "Sam" | Sequence of characters |
Notice the quotation marks:
25 is an integer.
"25" is a string.
True is a Boolean.
"True" is a string containing the characters T, r, u, e.
Sometimes a value is stored as one type, but you need to use it as another type. Casting means converting a value from one data type to another.
Casting
Casting is temporarily changing a value from one data type to another, usually so that a particular operation can be carried out.
Common examples in Python include:
| Cast | Meaning | Example |
|---|
int(...) | Convert to integer | int("15") gives integer 15 |
float(...) | Convert to real | float("1.75") gives real 1.75 |
str(...) | Convert to string | str(42) gives string "42" |
Casting is especially useful with user input. In Python, input() gives a string, even if the user types digits.
Converting input before calculating
A program asks the user for two numbers. The user types 12 and 3.
- The values from
input() are strings, so they are initially stored like "12" and "3".
- If the program uses
"12" + "3", it may concatenate the strings to make "123" instead of adding the numbers.
- Cast each input before calculating:
num1 = int(num1Text) and num2 = int(num2Text).
- Now
num1 + num2 gives integer 15, so the program can calculate correctly.
- If the result must be displayed inside a message, cast it back to a string, such as
"Total: " + str(total).
Thinking casting always changes the original
A cast usually produces a converted value. The original variable is not changed unless you store the converted value back into a variable.
For example, int(ageText) creates an integer version of ageText, but ageText itself is still a string unless you assign the result, such as age = int(ageText).
Casting must be possible
Not every cast makes sense. For example, trying to convert "twelve" into an integer would fail because it is not written as digits.
| Data | Best type | Reason |
|---|
17 as someone’s age | Integer | Whole number count |
2.5 hours | Real | Has a decimal part |
False for game over | Boolean | Only true or false |
"M" as a size code | Character | One symbol |
"M12 4AB" as a postcode | String | Text/code, not arithmetic |
In the exam
- Ask what the data represents, not just what it looks like. A phone number may look numeric but should usually be a string.
- Use the OCR terms: integer, real, Boolean, character, string and casting.
- When explaining casting, say why it is needed, such as converting input text to a number before doing a calculation.
Check yourself
- What data type would you choose for a product price, and why?
- Why is
"123" not the same as 123 in a program?
- Give one situation where casting would be useful.