You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson covers variables, constants, and the fundamental data types as required by OCR J277 Section 2.3. These concepts form the foundation of all programming and are essential for both Paper 1 (theory) and Paper 2 (practical programming) of the OCR GCSE Computer Science exam.
A variable is a named location in memory that stores a value which can change during the execution of a program. Think of a variable as a labelled box — the label is the variable name, and the contents of the box is the value.
OCR Pseudocode:
name = "Alice"
age = 15
name = "Bob"
Python:
name = "Alice"
age = 15
name = "Bob" # The value of name has changed from "Alice" to "Bob"
Key rules for variable names:
| Rule | Example | Valid? |
|---|---|---|
| Must start with a letter | score | Yes |
| Can contain letters, numbers, underscores | player_1 | Yes |
| Cannot start with a number | 1st_place | No |
| Cannot contain spaces | my score | No |
| Should be meaningful/descriptive | x vs totalScore | Both valid, but totalScore is better |
OCR Exam Tip: Always use meaningful variable names in your exam answers. Names like
xorawill not lose you marks but may make your code harder for the examiner to follow. Names liketotalScoreoruserNamedemonstrate good programming practice.
The way an assignment statement reads, evaluates, and stores a value into a named memory location is shown below. Notice that an assignment of a new value overwrites whatever was previously stored in that variable:
flowchart LR
A[Statement: name = 'Alice'] --> B[Evaluate the right-hand side]
B --> C[Result: 'Alice']
C --> D[Find or create memory slot named 'name']
D --> E[Store 'Alice' in slot]
E --> F[Later: name = 'Bob']
F --> G[Overwrite slot — old 'Alice' lost]
A constant is a named value that is set once and cannot change during the execution of a program. Constants are used for values that remain fixed, such as the value of pi or the VAT rate.
OCR Pseudocode:
const VAT_RATE = 0.20
const PI = 3.14159
price = 100
total = price + (price * VAT_RATE)
print(total)
Python:
VAT_RATE = 0.20 # Python has no true const keyword; convention uses UPPER_CASE
PI = 3.14159
price = 100
total = price + (price * VAT_RATE)
print(total) # Output: 120.0
| Reason | Explanation |
|---|---|
| Readability | VAT_RATE is clearer than 0.20 scattered throughout code |
| Maintainability | Change the value in one place rather than every occurrence |
| Prevents accidental changes | The value cannot be modified during execution |
OCR Exam Tip: In the exam, if a question asks "why might a programmer use a constant instead of a variable?", always mention that it prevents accidental modification and makes the program easier to maintain.
A data type defines the kind of value a variable can hold and what operations can be performed on it. The OCR specification requires knowledge of five data types:
| Data Type | Description | Example Values |
|---|---|---|
| Integer | A whole number (positive, negative, or zero) | 42, -7, 0 |
| Real (float) | A number with a decimal point | 3.14, -0.5, 100.0 |
| Boolean | A value that is either true or false | true, false |
| Character | A single letter, digit, or symbol | 'A', '7', '@' |
| String | A sequence of characters (text) | "Hello", "GCSE", "" |
age = 15 // integer
temperature = 36.6 // real
isLoggedIn = true // Boolean
initial = 'J' // character
fullName = "John Smith" // string
age = 15 # int
temperature = 36.6 # float
is_logged_in = True # bool (note: capital T)
initial = 'J' # str (Python doesn't distinguish char from string)
full_name = "John Smith" # str
Sometimes you need to convert a value from one data type to another. This is called casting.
| Conversion | OCR Pseudocode | Python |
|---|---|---|
| String to integer | int("42") | int("42") |
| String to real | real("3.14") | float("3.14") |
| Integer to string | str(42) | str(42) |
| Real to integer | int(3.7) → 3 | int(3.7) → 3 |
| ASCII to character | ASC(65) → 'A' | chr(65) → 'A' |
| Character to ASCII | CHR('A') → 65 | ord('A') → 65 |
OCR Exam Tip: A very common error is forgetting to cast the result of
input()to an integer. In Python,input()always returns a string. To do arithmetic, you must writeint(input())orfloat(input()). This is also true in OCR pseudocode.
const in OCR pseudocode.OCR Exam Tip: If asked to identify the data type of a value, look carefully.
"42"is a string (because of the quotes), but42is an integer.Trueis a Boolean, not a string. This distinction is frequently tested.
This worked example uses variables (the changing prices), a constant (the VAT rate), and several data types (integer, real, string) together in one program. It shows how the choice of data type affects the calculation.
OCR reference language pseudocode:
const VAT_RATE = 0.20
itemName = "Keyboard"
quantity = 2
pricePerItem = 24.99
isInStock = true
subtotal = quantity * pricePerItem
vat = subtotal * VAT_RATE
total = subtotal + vat
if isInStock == true then
print("Item: " + itemName)
print("Subtotal: £" + str(subtotal))
print("VAT: £" + str(vat))
print("Total: £" + str(total))
else
print("Item is out of stock.")
endif
Step-by-step trace:
| Variable | Data type | Value after assignment |
|---|---|---|
| VAT_RATE | real (constant) | 0.20 |
| itemName | string | "Keyboard" |
| quantity | integer | 2 |
| pricePerItem | real | 24.99 |
| isInStock | Boolean | true |
| subtotal | real | 2 * 24.99 = 49.98 |
| vat | real | 49.98 * 0.20 = 9.996 |
| total | real | 49.98 + 9.996 = 59.976 |
Output:
Item: Keyboard
Subtotal: £49.98
VAT: £9.996
Total: £59.976
Key observations:
The equivalent Python version:
VAT_RATE = 0.20 # constant by convention (uppercase)
item_name = "Keyboard"
quantity = 2
price_per_item = 24.99
is_in_stock = True
subtotal = quantity * price_per_item
vat = subtotal * VAT_RATE
total = subtotal + vat
if is_in_stock:
print(f"Item: {item_name}")
print(f"Subtotal: £{subtotal:.2f}")
print(f"VAT: £{vat:.2f}")
print(f"Total: £{total:.2f}")
else:
print("Item is out of stock.")
Note: Python has no true const keyword — the UPPER_CASE naming convention signals to other programmers that the value should not be changed.
Common misconception: Students often confuse "42" (a string containing the digits 4 and 2) with 42 (the integer forty-two). The quotes matter! In expressions, "3" + "4" gives "34" (string concatenation), while 3 + 4 gives 7 (integer addition). Similarly, true is a Boolean (two letters capitalised in Python as True) while "true" with quotes is a string. When an exam asks you to "identify the data type", look carefully at the quote marks and case, and remember that input() always returns a string — you must cast it to int or real before doing arithmetic.
Exam question (4 marks): State the most appropriate data type for each of the following variables in a library management system, and justify your choice. (a) The ISBN of a book (13 digits) (b) The number of books currently on loan (c) Whether a book has been returned (d) The title of a book
Grade 3-4 answer (1/4 marks):
(a) integer (b) integer (c) integer (d) string
Examiner commentary: Correct types for (b) and (d), but no justifications given. (a) is wrong — even though ISBNs look like numbers, storing them as an integer would drop any leading zeros. (c) should be Boolean. Earns 1 mark.
Grade 5-6 answer (3/4 marks):
(a) String, because ISBNs can contain leading zeros. (b) Integer, because the number of books must be a whole number. (c) Boolean, because it is either true or false. (d) String, because a title is text.
Examiner commentary: All four types correct and brief justifications given. Earns 3 marks (loses 1 for not fully explaining why integer is more appropriate than real for counts, or why a book title cannot be a character type).
Grade 7-9 answer (4/4 marks):
(a) String. Although an ISBN consists of digits, it should be stored as a string because (i) it can contain a leading zero which an integer would discard, and (ii) no arithmetic is ever performed on ISBNs. (b) Integer. You cannot have 2.5 books on loan — counts are always whole numbers. Real would waste memory and allow invalid values. (c) Boolean. The two states (returned / not returned) map exactly to true and false. Using a string like "yes"/"no" would allow typos. (d) String. A title is a sequence of characters, so string is the correct type. A single character type only holds one symbol and would be too small.
Examiner commentary: Full marks. Each answer includes a clear justification that demonstrates understanding of the properties of the data type (integer is whole, Boolean is two-valued, string is multi-character) and the semantics of the data being stored. This is the top-band approach: pair the correct answer with a reason.
Examiner insight: Data type questions are often "low-effort, high-reward" — a quick way to bank marks if you reason carefully. The trap is that ISBN-style identifiers, phone numbers, and postcodes look numeric but should be stored as strings because they can contain leading zeros, dashes, spaces, or letters (UK postcodes like "SW1A 1AA"). Whenever a value is an identifier rather than a quantity you would do arithmetic on, choose string.
To strengthen your understanding of variables, constants, and data types, work through the following exercises:
Identify the data type. For each value below, state which of the five OCR data types it is.
When to use a constant. Identify which of the following values should be stored as a constant rather than a variable: pi, the user's score in a game, the speed of light, today's date, the VAT rate, the current temperature reading from a sensor. (Constants: pi, speed of light, VAT rate. Variables: score, date, temperature.)
Casting pitfalls. What does each line below print?
Design a record. Suppose you are designing a structure to hold information about a library book. List at least five fields, their data types, and justify your choices. For example: title (string), ISBN (string — leading zeros), numberOfPages (integer — whole number), isOnLoan (Boolean — two states), averageRating (real — decimal allowed).
These exercises mirror the style of short-answer questions on OCR Paper 1 (theory) and help build the data type vocabulary you will use in every programming question on Paper 2.
This content is aligned with OCR GCSE Computer Science (J277) specification section 2.2 Programming fundamentals. For the most accurate and up-to-date information, please refer to the official OCR specification document.