Loading…
Loading…
Loading…
OCR GCSE Computer Science · J277
OCR J277 Check the specification (PDF) (opens in a new tab)
A program might store a person's name, the number of items they order and whether their order is complete. These values have different meanings, so they need different data types. A data type describes what kind of value is represented and which operations make sense for it.
For example, 12 is a number that can be used in arithmetic. "12" is text containing two digit characters. Although they look similar, a program does not treat them as interchangeable.
A variable is a name associated with a stored value. In Python, an assignment such as quantity = 12 stores the integer value 12 under the name quantity.
An integer is a whole number: positive, negative or zero. Counts such as goals scored or completed laps normally need integers because a count does not include a fractional part.
A real value can have a fractional part, such as 12.47 seconds or a temperature of -2.5 degrees Celsius. Python represents real-number values using its float type. A float can also represent a value such as 5.0: the value need not have a non-zero fractional part.
A Boolean has one of two logical values, True or False. It is useful for recording whether something is the case, such as whether a door is locked. In Python, these values have capital initial letters and no quotation marks. "True", with quotation marks, is text rather than a Boolean value.
A character is one symbol, such as "A", "7" or "?". A string is a sequence of characters, such as "Aisha" or "AB12 3CD". Python has no separate character type: it represents a character using a string containing one character.
These assignments show how the types are used in Python:
goals = 3 # integer
finishing_time = 12.47 # float: represents a real value
door_locked = True # Boolean
grade = "A" # character data: one-character string
name = "Aisha" # string
The quotation marks identify text in the program; they are not part of the stored name or grade.
Choose a data type by considering the values that must be represented and what the program will do with them.
| Data to represent | Suitable type | Reason |
|---|---|---|
| Number of goals scored | Integer | Goals are counted in whole numbers. |
| Runner's finishing time | Real | The time may include fractions of a second. |
| Whether a booking is complete | Boolean | There are two logical states: complete or not complete. |
| A single-letter grade | Character | Only one symbol is needed. |
| An email address | String | It contains a sequence of letters and other characters. |
Digits do not automatically mean that data should be numeric. A postcode is a string because it is an identifier, not a quantity used in arithmetic. Similarly, a telephone number is usually stored as text: its digits identify a contact, and any leading zero must be preserved.
An unsuitable type can lose information or prevent an operation. An integer cannot represent a finishing time of 12.47 seconds accurately because it cannot retain the fractional part. A real value is more suitable.
Casting converts a value from one data type to another. This is particularly useful with keyboard input: Python's input() returns a string, even when the user types digits.
Common Python casts include:
| Expression | Converted value | Resulting type |
|---|---|---|
int("100") | 100 | Integer |
float("100") | 100.0 | Float, representing a real value |
str(100) | "100" | String |
int(5.7) | 5 | Integer |
Converting a real value to an integer discards its fractional part; it does not round to the nearest whole number. Choose the conversion according to the data you need to preserve.
Casting can provide a temporary converted value without changing the original variable:
age_text = "19"
old_enough = int(age_text) >= 18
Here, int(age_text) provides the integer 19 for the comparison with 18. The comparison produces the Boolean value True, stored in old_enough. Afterwards, age_text still contains the string "19".
To retain a converted value, assign it to a variable:
age = int(age_text)
Now age contains an integer, while age_text still contains a string. Assigning the converted result back to the original name, as in age_text = int(age_text), would replace its stored text value with an integer.
A shop needs a price that may include pence and a quantity that must be a whole number. The following Python program converts each input before calculating the order total:
price = float(input("Enter price in pounds: "))
quantity = int(input("Enter quantity: "))
total = price * quantity
print(total)
If the user enters 4.50 and 3, the program displays 13.5, representing £13.50. float() preserves the fractional price, while int() supplies a whole-number quantity. The multiplication therefore uses numeric values rather than the text returned by input().
Casting is not needed just because data comes from the keyboard. A name or postcode should remain a string. Conversion is useful when the current type differs from the type needed for the intended operation.
Get unlimited access to all revision notes, key terms, and exam tips.
float.True or False; a logical state.Choose from the data's meaning and required operations, not just its appearance. Digits in a postcode do not make it a number.
input() returns a string. Convert it when numeric processing is needed:
int("12") → 12float("12.5") → 12.5str(12) → "12"int(5.7) → 5: discards the fractional part, not rounding.int(age_text) >= 18 uses a converted value for a comparison; age_text remains text.age = int(age_text) stores the integer in another variable.age_text = int(age_text) replaces the original variable's value with an integer.Get unlimited access to all revision notes, key terms, and exam tips.
When justifying a data type, name it and link it to the values needed: for example, ‘Real, because a finishing time may include fractions of a second.’
Distinguish values from text: 7 is an integer, but "7" is character data represented as a one-character string in Python.
In Python, use float() for conversion to a real value. Do not confuse Python syntax with OCR Exam Reference Language, which also supports real().
Trace assignments carefully: a cast produces a converted value, but the original variable only changes if you assign the result back to it.
Data type
A classification describing the kind of value represented and the operations that can be performed on it.
Integer
A data type for whole numbers, including positive numbers, negative numbers and zero.
Real
A numeric data type that can represent values with a fractional part. Python uses the float type for real-number values.
Boolean
A data type with two logical values: True and False.
Character
A data type representing one symbol, such as a letter, digit or punctuation mark. Python represents character data using a string of length one.
String
A data type representing a sequence of characters, such as a name or postcode.
Casting
Converting a value from one data type to another so that it can be used in a different way.
Put your knowledge into practice — try past paper questions for Computer Science
Data type
A classification describing the kind of value represented and the operations that can be performed on it.
Integer
A data type for whole numbers, including positive numbers, negative numbers and zero.
Real
A numeric data type that can represent values with a fractional part. Python uses the float type for real-number values.
Boolean
A data type with two logical values: True and False.
Character
A data type representing one symbol, such as a letter, digit or punctuation mark. Python represents character data using a string of length one.
String
A data type representing a sequence of characters, such as a name or postcode.
Casting
Converting a value from one data type to another so that it can be used in a different way.