SLIDE 1
CSZone.co.uk
Click to reveal · Arrow keys also work
OCR J277 · Component 2 · Topic 2.2.1a

Programming Fundamentals
Variables, Constants & Data Types

Variable · Constant · Integer · Real · Boolean · Character · String · Casting · Operators

CSZone OCR GCSE Computer Science J277
Learning Objectives

By the end of this video you will be able to...

Define a variable and a constant — explain the difference and when to use each one
Identify and choose between the 5 data types: Integer, Real, Boolean, Character and String — and justify your choice
Understand casting — why it is needed, especially because input() always returns a String in Python
Recognise and use all arithmetic, comparison and Boolean operators on the OCR J277 spec
Write assignment, input and output statements correctly in both OCR ERL pseudocode and Python — and avoid the classic exam mistakes
⚡ Topics 2.2.1 and 2.2.2 are combined here. The OCR exam tests variables, constants, operators and data types together.
Variables

Variables — named memory locations

DEFINITION
A variable is a named location in memory that stores a value which can change during program execution. The name stays fixed; the value inside can be overwritten at any time.
ASSIGNMENT SYNTAX
OCR ERL:  name ← value
Python:   name = value
NAMING CONVENTIONS
Use meaningful names — no spaces. Use camelCase (playerScore) or underscore (player_score). Avoid single letters unless writing a loop counter.
PSEUDOCODE & PYTHON EXAMPLES
OCR ERL
score0 playerName"Alice" scorescore + 10 ← value changes — that's fine
PYTHON
score = 0 player_name = "Alice" score = score + 10 # score is now 10
⚡ The right-hand side is always evaluated first, then stored in the variable on the left. So score ← score + 10 reads the current value of score, adds 10, and puts the result back into score.
Constants

Constants — fixed values that cannot change

DEFINITION
A constant is a named value that is set once and cannot change during program execution. Unlike a variable, reassigning a constant should not be allowed.
WHY USE CONSTANTS?
Code is easier to readTAX_RATE is clearer than a bare 0.2 scattered through the program
Change the value once in one place — it updates everywhere in the program automatically
Prevents accidental modification of values that must stay fixed
OCR ERL — uses const keyword
const TAX_RATE0.2 const MAX_LIVES3 const PI3.14159 const PASS_MARK50
PYTHON — UPPER_CASE convention only
TAX_RATE = 0.2 MAX_LIVES = 3 PI = 3.14159 PASS_MARK = 50
⚠ EXAM NOTE
Python has no built-in constant keyword — UPPER_CASE is a naming convention only. OCR ERL uses the const keyword. In exam pseudocode, always write const.
Data Types

The five data types — 2.2.2

Integer
Whole numbers — no decimal point
age = 16    score = -5    count = 0
Real / Float
Numbers with a decimal point
price = 4.99    temp = 36.7
Boolean
Only True or False — used in conditions and flags
game_over = False    is_valid = True
Character
Single character only — one letter, digit or symbol
grade = 'A'    key = '?'
String
Sequence of zero or more characters
name = "Alice"    msg = ""
EXAM TIP — CHOOSING THE RIGHT TYPE
Number of students → Integer
Temperature → Real
Logged in? → Boolean
Menu option letter → Character
Full name → String
Casting

Casting — temporarily converting data types

WHAT IS CASTING?
Casting temporarily converts a value from one data type to another. The string "42" is not the same as the integer 42 — you must cast to use it in arithmetic.
⚠ WHY CASTING IS NEEDED
input() always returns a String — even when the user types a number. You must cast to Integer or Real before doing arithmetic, or Python will throw a TypeError at runtime.
CASTING FUNCTIONS — same in ERL and Python
int(x)
→ Integer
float(x)
→ Real
str(x)
→ String
bool(x)
→ Boolean
WITHOUT CASTING — BREAKS AT RUNTIME
age = input("Enter age: ") # String! years_left = 65 - age # ❌ TypeError
WITH CASTING — CORRECT
age = int(input("Enter age: ")) # Integer ✓ years_left = 65 - age # ✅ works
OCR ERL EQUIVALENT
ageint(input("Enter age: ")) yearsLeft65 - age
Arithmetic Operators

Arithmetic operators — + − * / MOD DIV ^

STANDARD OPERATORS
+Addition5 + 3 → 8
-Subtraction10 - 4 → 6
*Multiplication6 * 7 → 42
/Division (real result)7 / 2 → 3.5
INTEGER & POWER OPERATORS
MODRemainder after division10 MOD 3 → 1
DIVInteger quotient10 DIV 3 → 3
^Exponentiation (power)2 ^ 8 → 256
MOD vs DIV — MEMORY AID
Think of 17 ÷ 5: it goes 3 times with 2 left over.
17 DIV 5 → 3 (how many times it fits)
17 MOD 5 → 2 (the remainder)
⚠ PYTHON USES DIFFERENT SYMBOLS
ERL MOD → Python %
ERL DIV → Python //
ERL ^   → Python **
OCR exams use ERL notation — use MOD, DIV and ^ unless told otherwise.
# OCR ERL result17 MOD 5 ← 2 result17 DIV 5 ← 3 result2 ^ 8 ← 256
Comparison Operators

Comparison operators — == != < <= > >=

EQUALITY & LESS-THAN
==Equal to5 == 5 → True
!=Not equal to5 != 3 → True
<Less than3 < 7 → True
<=Less than or equal to5 <= 5 → True
GREATER-THAN
>Greater than9 > 4 → True
>=Greater than or equal to3 >= 3 → True
WHAT THEY RETURN
Comparison operators always return a BooleanTrue or False. Nothing else. This is why they work inside IF conditions and WHILE loops.
OCR ERL EXAMPLE
IF score >= 50 THEN print("Pass") ELSE print("Fail") ENDIF
== checks equality; (ERL) or = (Python) is assignment. Never use a single = inside an IF condition.
Boolean Operators

Boolean operators — AND, OR, NOT

AND
BOTH conditions must be True
True AND True → True
True AND False → False
IF age >= 16 AND hasID == True THEN
OR
At least ONE condition must be True
True OR False → True
False OR False → False
IF choice == "Y" OR choice == "y" THEN
NOT
REVERSES the Boolean value
NOT True → False
NOT False → True
IF NOT gameOver THEN
COMBINED EXAMPLE — OCR ERL
IF (score >= 40 AND attended == True) OR bonus == True THEN print("Eligible for certificate") ENDIF WHILE NOT gameOver DO playRound() ENDWHILE
⚡ Use brackets to make the intended order of evaluation clear — (A AND B) OR C is different from A AND (B OR C). When in doubt, bracket it.
Input · Output · Assignment

Input, output and assignment

INPUT
OCR ERL
nameinput("Enter name: ") ageint(input("Enter age: "))
PYTHON
name = input("Enter name: ") age = int(input("Enter age: "))
OUTPUT
OCR ERL
print(name) print("Hello, " + name)
PYTHON
print(name) print("Hello,", name)
ASSIGNMENT
The right-hand side is evaluated first, then stored in the variable on the left.
ERL: variable ← expression
Python: variable = expression
FULL WORKED EXAMPLE — OCR ERL
nameinput("Enter your name: ") ageint(input("Enter your age: ")) yearsLeft65 - age print(name + " retires in " + str(yearsLeft) + " years")
⚡ To concatenate an integer into a string for output, cast it back to String with str() first — otherwise you get a TypeError.
Exam Practice

Variables, constants & data types — exam questions

Question 1 — 1 mark
Which data type stores only the values True or False?
Answer — Q1
Boolean — stores only True or False. (1 mark)
Question 2 — 2 marks
A Python program uses age = input("Enter age: "). Explain why casting is needed before calculating how many years until retirement age of 65.
Answer — Q2
input() always returns a String [1] — a String cannot be used in arithmetic (subtracted from 65), so it must be cast to an Integer using int() first [1]
Question 3 — 3 marks
Write OCR ERL pseudocode to ask the user for a number, store it as an integer called num, then output the square of num. Use the correct assignment operator and exponentiation operator for OCR ERL.
Exam Answers

Question 3 — answer and mark scheme

Q3 MARK SCHEME
numint(input("Enter a number: ")) ← [1] input used ← [1] int() cast applied print(num ^ 2) ← [1] ^ for exponentiation
Also accept: print(num * num) for the third mark.
COMMON MARK LOSSES ON THIS Q
• Using num = ... instead of num ← ... (wrong assignment operator for ERL)
• Using ** instead of ^ (Python operator, not ERL)
• Forgetting the int() cast around input()
OCR ERL vs PYTHON OPERATORS
Operation OCR ERL Python
Exponentiation^**
Integer divisionDIV//
ModulusMOD%
Assignment=
CASTING QUICK REFERENCE
Target type ERL function Python
Integerint(x)int(x)
Realfloat(x)float(x)
Stringstr(x)str(x)
⚡ The exam will often give you Python code and ask about OCR ERL, or vice versa. Always check which language is specified — operators and assignment syntax differ. is ERL only; = is Python only.
Common Mistakes

Common mistakes — avoid these in the exam

MISTAKE 1 — Wrong assignment operator in OCR ERL
Writing x = 5 in OCR ERL pseudocode — this is Python syntax, not ERL
✓ OCR ERL requires the arrow: x ← 5
MISTAKE 2 — Forgetting to cast input()
Writing age = input("Age: ") then doing arithmetic — causes a TypeError at runtime
✓ Always cast on the same line: age = int(input("Age: "))
MISTAKE 3 — Confusing MOD and DIV
Saying 10 MOD 3 = 3 — MOD gives the remainder, not the quotient
10 MOD 3 = 1 (remainder)  |  10 DIV 3 = 3 (quotient)
MISTAKE 4 — Mixing up ERL and Python operator symbols
Using 2 ** 8 in OCR ERL pseudocode, or 10 % 3 instead of 10 MOD 3
✓ ERL: ^  MOD  DIV  |  Python: **  %  //
Summary

Key points — 2.2.1a

Variables store changeable values (x ← 5); constants store fixed values (const MAX ← 10) and use the const keyword in OCR ERL
Five data types: Integer Real Boolean Character String — always choose the most appropriate type for the data being stored
Casting converts between types — int(), float(), str(); input() always returns a String — always cast before arithmetic
Arithmetic: + − * / MOD DIV ^  ·  Comparison: == != < <= > >=  ·  Boolean: AND OR NOT
OCR ERL: assign with , power with ^, remainder with MOD  ·  Python: assign with =, power with **, remainder with %
⚡ Next topic: 2.2.1b — Sequence, Selection & Iteration — IF statements, FOR loops and WHILE loops in detail.
2.2.1a Complete

Variables, Constants
& Data Types

Get the full resource pack at CSZone.co.uk

📄
Marked Worksheet
CSZone.co.uk
Quiz
CSZone.co.uk
📊
Slides
CSZone.co.uk
Next Up
2.2.1b — Sequence, Selection & Iteration