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.
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.