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.
The five data types
| Data type | What it stores | Examples |
|---|---|---|
| Integer | A whole number, positive, negative or zero | 7, -3, 0, 250 |
| Real (also called float) | A number with a decimal part | 3.14, 0.5, -2.75 |
| Boolean | One of two values: True or False | True, False |
| Character | A single letter, digit or symbol | "A", "7", "?" |
| String | A sequence of characters (text) | "Hello", "Cust001", "07700 900123" |
In the reference language, characters and strings are both written inside double quotes.
Why the type matters
The type decides what happens when you use an operator. Look at the difference.
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.
| Calculation | Result | Type of result |
|---|---|---|
| 7 / 2 | 3.5 | Real |
| 7 DIV 2 | 3 | Integer |
| 7 MOD 2 | 1 | Integer |
| 7 > 2 | True | Boolean |
Casting
| Function | What it does | Example | Result |
|---|---|---|---|
| str() | Casts to a string | str(345) | "345" |
| int() | Casts to an integer | int("3") | 3 |
| float() or real() | Casts to a real number | float("4.52") | 4.52 |
| bool() | Casts to a Boolean | bool("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.
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.
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.
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.
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.
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?
| Data | Suitable type | Reason |
|---|---|---|
| Number of goals scored | Integer | A whole number that is counted |
| Price of a sandwich, 3.49 | Real | Has a decimal part |
| Average test mark, 62.5 | Real | An average can have a decimal part |
| Whether a door is locked | Boolean | Only two possible values |
| First initial of a name | Character | A single letter |
| Surname | String | Text of any length |
| Phone number, 07700 900123 | String | See below |
| Postcode, AB1 2CD | String | Contains letters and a space |
| Student ID, S2041 | String | Contains a letter |
Why a phone number is a string
The same idea applies to postcodes, account numbers and ID numbers. If the digits are a label rather than an amount, use a string.
Data types in a program
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.