← OCR GCSE

OCR Exam Reference Language and Python side by side

Comment

// This is a comment
# This is a comment

Assignment

x = 3
name = "Louise"
x = 3
name = "Louise"

Constant

const vat = 0.2
VAT = 0.2  # constant by naming convention

Global variable

global userID = "Cust001"
userID = "Cust001"  # use "global userID" inside a function to change it

Input

name = input("Enter a name")
name = input("Enter a name")

Output

print("Hello " + name)
print("Hello " + name)

Casting

str(345)
int("3")
float("4.52")
real("4.52")
bool("True")
str(345)
int("3")
float("4.52")
float("4.52")
bool("True")

Comparison operators

==  !=  <  <=  >  >=
==  !=  <  <=  >  >=

Arithmetic operators

+  -  *  /  ^ (exponent)
+  -  *  /  ** (exponent)

Integer division (quotient) and remainder

17 DIV 5   // 3
17 MOD 5   // 2
17 // 5   # 3
17 % 5    # 2

Boolean operators

AND  OR  NOT
and  or  not

Count-controlled loop (for)

for i = 0 to 9
    print(i)
next i
for i in range(0, 10):
    print(i)

For loop with step

for i = 10 to 0 step -1
    print(i)
next i
for i in range(10, -1, -1):
    print(i)

Condition-controlled loop (while)

while answer != "Correct"
    answer = input("New answer")
endwhile
while answer != "Correct":
    answer = input("New answer")

Do until loop (runs at least once)

do
    answer = input("New answer")
until answer == "Correct"
while True:
    answer = input("New answer")
    if answer == "Correct":
        break

If, elseif, else

if answer == "Yes" then
    print("Correct")
elseif answer == "No" then
    print("Wrong")
else
    print("Error")
endif
if answer == "Yes":
    print("Correct")
elif answer == "No":
    print("Wrong")
else:
    print("Error")

Switch / case

switch day:
    case "Sat":
        print("Saturday")
    case "Sun":
        print("Sunday")
    default:
        print("Weekday")
endswitch
match day:
    case "Sat":
        print("Saturday")
    case "Sun":
        print("Sunday")
    case _:
        print("Weekday")
# or use if / elif / else

String length

subject.length
len(subject)

Substring (start index, number of characters)

subject.substring(3, 5)
subject[3:3 + 5]

Left and right characters

subject.left(4)
subject.right(3)
subject[:4]
subject[-3:]

Upper and lower case

subject.upper
subject.lower
subject.upper()
subject.lower()

Concatenation

print("Hello, " + name)
print("Hello, " + name)

ASCII conversion

ASC("A")   // 65
CHR(97)    // "a"
ord("A")   # 65
chr(97)    # "a"

Declare a 1D array

array colours[5]
array colours = ["Blue", "Pink", "Green"]
colours = [""] * 5
colours = ["Blue", "Pink", "Green"]

Assign to an array element

names[3] = "Noni"
names[3] = "Noni"

2D array

array gameboard[8,8]
gameboard[1,0] = "Pawn"
gameboard = [[""] * 8 for _ in range(8)]
gameboard[1][0] = "Pawn"

Procedure

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

greet("Sam")
def greet(name):
    print("Hello " + name)

greet("Sam")

Function

function squared(number)
    return number ^ 2
endfunction

newValue = squared(4)
def squared(number):
    return number ** 2

newValue = squared(4)

Random number

roll = random(1, 6)
r = random(-1.0, 10.0)
import random
roll = random.randint(1, 6)
r = random.uniform(-1.0, 10.0)

Create and open a file

newFile("myText.txt")
myFile = open("myText.txt")
myFile = open("myText.txt", "w")  # or "r" to read, "a" to append

Read a line / write a line

line = myFile.readLine()
myFile.writeLine("Add new line")
line = myFile.readline()
myFile.write("Add new line\n")

Read to end of file

while NOT myFile.endOfFile()
    print(myFile.readLine())
endwhile
for line in myFile:
    print(line.strip())

Close a file

myFile.close()
myFile.close()