GCSE Revision

Exam Reference Language

Every construct in OCR's Exam Reference Language (ERL), grouped as the specification groups them, with the Python that does the same. All the code in the exam is in ERL; you can answer in ERL or Python. For more practice, see the Exam Reference Language notes.

Operators

Comparison operators

a == b
a != b
a < b
a <= b
a > b
a >= b

The same in both: equal to, not equal to, less than (or equal to), greater than (or equal to).

Arithmetic operators

total = a + b - c * d / e

Addition, subtraction, multiplication and division are the same in both.

Exponent (^)

area = side ^ 2

Python uses ** for powers; ^ means something else in Python.

Modulo (MOD)

remainder = 17 MOD 5

The remainder after whole-number division: 17 MOD 5 is 2.

Quotient (DIV)

whole = 17 DIV 5

Whole-number division, ignoring the remainder: 17 DIV 5 is 3.

Boolean operators

if age >= 12 AND NOT member OR guest then
    print("Welcome")
endif

AND, OR and NOT are written in lower case in Python.

Comments

Comment

// Work out the total
total = price * quantity

Variables and constants

Assignment

score = 10
name = "Priya"

Constant

const vat = 0.2

Python has no constants: a name in capitals shows it shouldn't change.

Global variable

global high_score = 0

In Python, a variable made outside every function is global, but a function must say global before changing it.

Input and output

Input

name = input("What is your name?")

Both return what the user types as a string.

Output

print("Hello " + name)

Casting

To a string: str()

text = str(42)

To an integer: int()

age = int("15")

To a real number: float() and real()

price = float("2.50")
price = real("2.50")

ERL has real() as well as float(); Python only has float().

To a Boolean: bool()

paid = bool("True")

In Python, bool() of any non-empty string is True, even bool("False"), so compare the text instead.

Iteration

Count-controlled loop: for

for i = 1 to 5
    print(i)
next i

ERL's to includes the end value; Python's range stops one before it.

Count-controlled loop with step

for i = 10 to 0 step -2
    print(i)
next i

Condition-controlled loop: while

answer = ""
while answer != "yes"
    answer = input("Ready?")
endwhile

Checks the condition before each pass, so it may never run.

Condition-controlled loop: do … until

do
    answer = input("Ready?")
until answer == "yes"

Runs once before checking. Python has no do … until: run the body once, then use while with the opposite condition.

Selection

if … then … elseif … else

if mark >= 70 then
    print("Distinction")
elseif mark >= 50 then
    print("Pass")
else
    print("Try again")
endif

switch … case

switch day:
    case "Sat":
        print("Weekend")
    case "Sun":
        print("Weekend")
    default:
        print("School day")
endswitch

Python's usual equivalent is if, elif and else.

String handling

Length: .length

size = word.length

Substring: .substring(start, count)

part = word.substring(3, 4)

ERL's substring takes a start and a number of characters; a Python slice takes a start and a position to stop before. Both count from 0.

Left: .left(count)

start = word.left(3)

Right: .right(count)

end = word.right(2)

Concatenation: +

full = first + " " + last

Upper and lower case: .upper and .lower

shout = word.upper
quiet = word.lower

Python needs brackets after upper and lower.

Character codes: ASC() and CHR()

code = ASC("A")
letter = CHR(66)

ASC is ord in Python; CHR is chr.

File handling

Open and close: open() and .close()

file = open("scores.txt")
file.close()

Python's open() also takes a mode: "r" to read, "w" to write a new file, "a" to add to the end.

Read a line: .readLine()

line = file.readLine()

Write a line: .writeLine()

file.writeLine("Ada,18")

ERL's writeLine adds to the end of the file. Python's write doesn't start a new line, so add \n.

End of file: .endOfFile()

while NOT file.endOfFile()
    print(file.readLine())
endwhile

In Python, readline() returns an empty string at the end of the file.

Create a file: newFile()

newFile("notes.txt")

Arrays

Declare an array

array scores[5]

Arrays count from 0, so scores has positions 0 to 4. ERL arrays have a fixed length and one data type.

Declare an array with values

array colours = ["Red", "Green", "Blue"]

Declare a 2D array

array board[3, 3]

Assign to an array

scores[0] = 18
board[1, 2] = "X"

A 2D position is [row, column] in ERL and [row][column] in Python.

Sub programs

Procedure

procedure greet(name)
    print("Hello " + name)
endprocedure

Call a procedure

greet("Sam")

Function

function cube(number)
    return number ^ 3
endfunction

A function returns a value; Python writes procedures and functions with def.

Call a function

answer = cube(2)
print(cube(3))

Store or use the value a function returns.

Random numbers

Random numbers: random()

roll = random(1, 6)
amount = random(-1.0, 10.0)

Both ends are included. Whole numbers give a whole number; real numbers give a real number.