Python and Pseudocode Laboratory
Write it the Cambridge way, run it the Python way.
Paper 2 is answered in pseudocode, and Paper 4 is answered in Python. This laboratory keeps the two side by side, so a construct you can write in one is never a mystery in the other.
17 of 17 constructs
Declaring a variable
BasicsCambridge pseudocode
DECLARE Age : INTEGER Age ← 16
Python
age: int = 16
Cambridge pseudocode names the type at declaration. Python infers it, and the annotation is optional.
Declaring a constant
BasicsCambridge pseudocode
CONSTANT Pi = 3.142
Python
PI = 3.142
Python has no true constant. An upper-case name is a convention, not an enforcement.
Assignment
BasicsCambridge pseudocode
Total ← Total + 1
Python
total = total + 1
The left arrow is assignment, not equality. A double arrow (←) never means 'is equal to'.
Input and output
BasicsCambridge pseudocode
INPUT Name OUTPUT "Hello ", Name
Python
name = input("Name: ")
print("Hello", name)Python's input() always returns a string, so cast with int() or float() when you need a number.
IF statement
SelectionCambridge pseudocode
IF Mark >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Retry"
ENDIFPython
if mark >= 50:
print("Pass")
else:
print("Retry")Python uses indentation instead of ENDIF. The comparison operators are the same.
CASE statement
SelectionCambridge pseudocode
CASE OF Grade
"A" : OUTPUT "Distinction"
"B" : OUTPUT "Merit"
OTHERWISE : OUTPUT "Pass"
ENDCASEPython
match grade:
case "A":
print("Distinction")
case "B":
print("Merit")
case _:
print("Pass")match/case needs Python 3.10 or later. An if/elif chain is always accepted.
Count-controlled loop
IterationCambridge pseudocode
FOR Index ← 1 TO 10 STEP 2
OUTPUT Index
NEXT IndexPython
for index in range(1, 11, 2):
print(index)range stops before the second value, so TO 10 becomes range(1, 11).
Pre-condition loop
IterationCambridge pseudocode
WHILE Count < 5 DO
Count ← Count + 1
ENDWHILEPython
while count < 5:
count = count + 1The condition is tested before the body, so the body may never run.
Post-condition loop
IterationCambridge pseudocode
REPEAT
OUTPUT "Again?"
Answer ← USERINPUT
UNTIL Answer = "N"Python
while True:
print("Again?")
answer = input()
if answer == "N":
breakPython has no REPEAT. A while True with a break is the standard translation, and it runs the body at least once.
One-dimensional array
Arrays and stringsCambridge pseudocode
DECLARE Names : ARRAY[1:5] OF STRING Names[1] ← "Ada"
Python
names = [""] * 5 names[0] = "Ada"
Cambridge arrays are 1-based; Python lists are 0-based. Translate the index, do not copy it.
Two-dimensional array
Arrays and stringsCambridge pseudocode
DECLARE Grid : ARRAY[1:3, 1:4] OF INTEGER Grid[2, 3] ← 7
Python
grid = [[0] * 4 for _ in range(3)] grid[1][2] = 7
Build each row separately. [[0] * 4] * 3 shares one row object between all three.
String handling
Arrays and stringsCambridge pseudocode
Word ← "COMPUTER" OUTPUT LENGTH(Word) OUTPUT LEFT(Word, 3) OUTPUT MID(Word, 4, 2) OUTPUT UCASE(Word)
Python
word = "COMPUTER" print(len(word)) print(word[:3]) print(word[3:5]) print(word.upper())
MID starts at position 4 in pseudocode, which is index 3 in Python. Subtract one, and the length stays the same.
Procedure
SubprogramsCambridge pseudocode
PROCEDURE Greet(Name : STRING)
OUTPUT "Hello ", Name
ENDPROCEDUREPython
def greet(name):
print("Hello", name)A procedure returns nothing. Call it as a statement.
Function
SubprogramsCambridge pseudocode
FUNCTION Square(N : INTEGER) RETURNS INTEGER
RETURN N * N
ENDFUNCTIONPython
def square(n):
return n * nA function must return a value, and the call is used inside an expression.
Reading a text file
FilesCambridge pseudocode
OPENFILE "data.txt" FOR READ
WHILE NOT EOF("data.txt")
READFILE "data.txt", Line
OUTPUT Line
ENDWHILE
CLOSEFILE "data.txt"Python
with open("data.txt", "r") as f:
for line in f:
print(line.rstrip())The with block closes the file even if an error occurs. rstrip() removes the trailing newline.
Record type
RecordsCambridge pseudocode
TYPE Student
DECLARE Name : STRING
DECLARE Mark : INTEGER
ENDTYPE
DECLARE Pupil : Student
Pupil.Name ← "Ada"Python
from dataclasses import dataclass
@dataclass
class Student:
name: str
mark: int
pupil = Student("Ada", 0)A dataclass gives named fields with a generated constructor, which matches a record closely.
Exception handling
FilesCambridge pseudocode
TRY
Value ← Number / 0
CATCH DivideError
OUTPUT "Cannot divide by zero"
ENDTRYPython
try:
value = number / 0
except ZeroDivisionError:
print("Cannot divide by zero")Catch the specific error you expect, not everything. A bare except hides real faults.
Three rules that carry marks
- Arrays are 1-based in pseudocode and 0-based in Python. Translate the index, never copy it.
- Pseudocode is marked on meaning, not syntax. A clear loop with correct logic beats perfect formatting.
- In Paper 4, the program must run. Read the question's file names and output format literally.