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).
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.
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).
total = a + b - c * d / e
Addition, subtraction, multiplication and division are the same in both.
area = side ^ 2
Python uses ** for powers; ^ means something else in Python.
remainder = 17 MOD 5
The remainder after whole-number division: 17 MOD 5 is 2.
whole = 17 DIV 5
Whole-number division, ignoring the remainder: 17 DIV 5 is 3.
if age >= 12 AND NOT member OR guest then
print("Welcome")
endif
AND, OR and NOT are written in lower case in Python.
score = 10
name = "Priya"
const vat = 0.2
Python has no constants: a name in capitals shows it shouldn't change.
global high_score = 0
In Python, a variable made outside every function is global, but a function must say global before changing it.
name = input("What is your name?")
Both return what the user types as a string.
print("Hello " + name)
text = str(42)
age = int("15")
price = float("2.50")
price = real("2.50")
ERL has real() as well as float(); Python only has float().
paid = bool("True")
In Python, bool() of any non-empty string is True, even bool("False"), so compare the text instead.
for i = 1 to 5
print(i)
next i
ERL's to includes the end value; Python's range stops one before it.
for i = 10 to 0 step -2
print(i)
next i
answer = ""
while answer != "yes"
answer = input("Ready?")
endwhile
Checks the condition before each pass, so it may never run.
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.
if mark >= 70 then
print("Distinction")
elseif mark >= 50 then
print("Pass")
else
print("Try again")
endif
switch day:
case "Sat":
print("Weekend")
case "Sun":
print("Weekend")
default:
print("School day")
endswitch
Python's usual equivalent is if, elif and else.
size = word.length
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.
start = word.left(3)
end = word.right(2)
full = first + " " + last
shout = word.upper
quiet = word.lower
Python needs brackets after upper and lower.
code = ASC("A")
letter = CHR(66)
ASC is ord in Python; CHR is chr.
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.
line = file.readLine()
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.
while NOT file.endOfFile()
print(file.readLine())
endwhile
In Python, readline() returns an empty string at the end of the file.
newFile("notes.txt")
array scores[5]
Arrays count from 0, so scores has positions 0 to 4. ERL arrays have a fixed length and one data type.
array colours = ["Red", "Green", "Blue"]
array board[3, 3]
scores[0] = 18
board[1, 2] = "X"
A 2D position is [row, column] in ERL and [row][column] in Python.
procedure greet(name)
print("Hello " + name)
endprocedure
greet("Sam")
function cube(number)
return number ^ 3
endfunction
A function returns a value; Python writes procedures and functions with def.
answer = cube(2)
print(cube(3))
Store or use the value a function returns.
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.
Comments
Comment
ERL
Python