The five data types
A data type tells the computer what kind of value a variable holds, how much memory to set aside for it, and what operations are allowed on it. The specification names five, and the skill assessed is choosing a suitable one for data in a given scenario.
Two choices catch people out, and both come up regularly.
A telephone number is a string, not an integer. It can begin with a zero, which an integer would discard, and no arithmetic is ever performed on it — nobody adds two phone numbers together. The same reasoning applies to a postcode, a card number and a student ID.
A price is a real, not an integer, because £19.99 has a fractional part. But a number of items is an integer, because half an item cannot be bought. Read the scenario carefully: the question is not what the value looks like but what it means.
- Data type
- a description of what kind of value a variable holds, how much memory to set aside for it, and what operations are allowed on it
- Integer
- a whole number data type
- Real
- a data type for a number with a decimal part
- Boolean
- a data type holding one of two values, true or false
- String
- a data type holding a sequence of characters
Casting
Casting temporarily changes a value from one data type to another. The specification asks you to understand that data types may be temporarily changed through casting, and where this may be useful.
The usual need arises at input. Whatever a user types arrives as a string, even when it looks like a number. If the program then tries to do arithmetic with it, either it fails or — worse — the values are joined together as text: "5" + "3" gives "53", not 8. Casting the input to an integer first fixes this.
The reverse need arises at output. Joining a number onto a string requires the number to be cast to a string first, since a string and an integer cannot be concatenated directly.
Worked example
This code is meant to add ten to a number the user enters, but it prints the wrong thing. Explain why and correct it. number = input("Enter a number") print(number + 10)
- Consider what input returns: a string, regardless of what the user typed.
- So number holds "7", not 7 — text that happens to look like a number.
- Adding an integer to a string either causes an error or joins them as text, giving "710".
- The fix is to cast the input to an integer before using it in arithmetic.
- The printed result then needs to be a string, so cast it back for output.
Answer: input returns a string, so the program is trying to add 10 to text rather than to a number. Casting fixes it: number = int(input("Enter a number")) print(str(number + 10))
Common mistake
Casting does not change the variable permanently, and it does not round. int(7.9) gives 7 — the fractional part is cut off, not rounded to 8.
- Casting
- temporarily changing a value from one data type to another, most often needed at input because whatever a user types arrives as a string
Before you practise
- The five types: integer, real, Boolean, character, string.
- Integer for whole numbers; real for values with a fractional part.
- Boolean for anything with exactly two states.
- Character is a single symbol; string is a sequence of them.
- Phone numbers, postcodes and IDs are strings — they may begin with zero and are never used in arithmetic.
- Justify a choice by what the data means, not what it looks like.
- Input always arrives as a string, so cast it before doing arithmetic.
- Cast a number to a string to join it onto text for output.
- int() truncates rather than rounds.