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.

16 constructs3 trace exercisesNo installation needed

17 of 17 constructs

Declaring a variable

Basics

Cambridge 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

Basics

Cambridge 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

Basics

Cambridge 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

Basics

Cambridge 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

Selection

Cambridge pseudocode

IF Mark >= 50 THEN
    OUTPUT "Pass"
ELSE
    OUTPUT "Retry"
ENDIF

Python

if mark >= 50:
    print("Pass")
else:
    print("Retry")

Python uses indentation instead of ENDIF. The comparison operators are the same.

CASE statement

Selection

Cambridge pseudocode

CASE OF Grade
    "A" : OUTPUT "Distinction"
    "B" : OUTPUT "Merit"
    OTHERWISE : OUTPUT "Pass"
ENDCASE

Python

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

Iteration

Cambridge pseudocode

FOR Index ← 1 TO 10 STEP 2
    OUTPUT Index
NEXT Index

Python

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

Iteration

Cambridge pseudocode

WHILE Count < 5 DO
    Count ← Count + 1
ENDWHILE

Python

while count < 5:
    count = count + 1

The condition is tested before the body, so the body may never run.

Post-condition loop

Iteration

Cambridge pseudocode

REPEAT
    OUTPUT "Again?"
    Answer ← USERINPUT
UNTIL Answer = "N"

Python

while True:
    print("Again?")
    answer = input()
    if answer == "N":
        break

Python 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 strings

Cambridge 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 strings

Cambridge 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 strings

Cambridge 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

Subprograms

Cambridge pseudocode

PROCEDURE Greet(Name : STRING)
    OUTPUT "Hello ", Name
ENDPROCEDURE

Python

def greet(name):
    print("Hello", name)

A procedure returns nothing. Call it as a statement.

Function

Subprograms

Cambridge pseudocode

FUNCTION Square(N : INTEGER) RETURNS INTEGER
    RETURN N * N
ENDFUNCTION

Python

def square(n):
    return n * n

A function must return a value, and the call is used inside an expression.

Reading a text file

Files

Cambridge 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

Records

Cambridge 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

Files

Cambridge pseudocode

TRY
    Value ← Number / 0
CATCH DivideError
    OUTPUT "Cannot divide by zero"
ENDTRY

Python

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.

Cambridge International AS & A Level Computer Science 9618 · examinations 2028 · personal study site

Official Cambridge page