Python and Pseudocode Laboratory

Write it the Cambridge way, run it the Python way.

Paper 2 is answered in pseudocode. Paper 4 is answered in a programming language of your choice; this platform teaches it in Python, so both are shown side by side and a construct you can write in one is never a mystery in the other.

17 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

One left arrow, ←, means assignment. It is a single arrow, not a double arrow, and it never means 'is equal to'. Equality is tested with = inside a condition, which is why Python needs == in the same place.

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. Equality is the operator that changes: Cambridge pseudocode tests equality with = and inequality with <>, while Python tests equality with == and inequality with !=. A single = in Python assigns, so it cannot appear in a condition.

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?"
    INPUT Answer
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"

Array bounds come from the declaration: ARRAY[1:5] runs from 1 to 5 and ARRAY[0:4] runs from 0 to 4. Python lists always start at index 0, so translate the index rather than copying 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") DO
    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("\n"))

A WHILE that tests EOF still takes DO, like every other WHILE. The with block closes the file even if an error occurs. Each line Python reads keeps its newline character, so rstrip("\n") removes the trailing line break and nothing else; a bare rstrip() would also strip trailing spaces and tabs, which may be part of the data you are asked to keep.

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.

Guarding against errors

Files

Cambridge pseudocode

IF Divisor <> 0 THEN
    Value ← Number / Divisor
ELSE
    OUTPUT "Cannot divide by zero"
ENDIF

Python

try:
    value = number / divisor
except ZeroDivisionError:
    print("Cannot divide by zero")

Exception handling is examined in the practical context: in Paper 4 you write it in your chosen language, where try/except naming a specific error keeps genuine faults visible. In a written pseudocode answer, guard the operation with a condition first, because the guide's constructs do not include a TRY block. Check the guide version issued with your syllabus before using any exception keyword in an answer.

Three rules that carry marks

  • A Cambridge array takes whatever lower bound its declaration gives: ARRAY[1:5] runs from 1 to 5 and ARRAY[0:4] runs from 0 to 4. Index 1 is a common choice, not a rule, so read the declaration before writing the loop. Python lists always start at index 0, so translate the index rather than copying it.
  • Marks come from the logic the mark scheme asks for, not from spelling. But the construct has to be recognisable: an IF without THEN, a WHILE without DO, or a loop with no NEXT can lose the mark, because the examiner cannot see the structure you intended.
  • In Paper 4 the program must run. Read the question's file names and output format literally. Python is this platform's teaching language; Cambridge also accepts Java and Visual Basic .NET.

The 17 constructs above follow the Cambridge pseudocode guide for teachers, 2027 to 2029. Python is the language this platform teaches for Paper 4; Cambridge accepts Python, Java or Visual Basic .NET, and the written papers are answered in pseudocode whichever language you choose.

Production information

Release
2026.11.28.2
Deployed (UTC)
29 Nov 2026 · 08:00 UTC
Curriculum
9618 · 2027–2029
Resource manifest
RES-MANIFEST-2026.11.28
Resources verified
28 Nov 2026

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

Official Cambridge page