These functions are part of the core language and require no import statement.
| Python | Pseudocode (Cambridge) | Java | Visual Basic |
|---|---|---|---|
print(...) | OUTPUT ... | System.out.println(...); | Console.WriteLine(...) |
input() | READ ... | new Scanner(System.in).nextLine(); | Console.ReadLine() |
len(seq) | SIZE(seq) | seq.length (arrays) / list.size() | seq.Length |
range(start, stop, step) | FOR i ← start TO stop‑1 STEP step | for (int i = start; i < stop; i += step) { … } | For i As Integer = start To stop - 1 Step step |
type(obj) | no direct equivalent – use documentation | obj.getClass() | obj.GetType() |
int(x), float(x), str(x) | INTEGER(x), REAL(x), STRING(x) | (int) x, (double) x, String.valueOf(x) | CInt(x), CDbl(x), CStr(x) |
abs(x), pow(x, y) | ABS(x), POWER(x, y) | Math.abs(x), Math.pow(x,y) | Math.Abs(x), Math.Pow(x,y) |
sum(iterable) | SUM(seq) | IntStream.of(arr).sum() | Enumerable.Sum(seq) |
# Decimal → Binarybin(13) # '0b1101'
# Binary → Decimal
int('1101', 2) # 13
# Decimal → Hex
hex(255) # '0xff'
# Hex → Decimal
int('FF', 16) # 255
# Left shift (multiply by 2ⁿ)x = 5 # 0b0101
x << 2 # 0b010100 = 20
# Right shift (integer division by 2ⁿ)
x >> 1 # 0b0010 = 2
# Bitwise AND, OR, XOR
a & b # 0b0100
a | b # 0b0111
a ^ b # 0b0011
| Gate | Symbol | Truth Table |
|---|---|---|
| AND | & | A B | A∧B |
| OR | ∨ | A B | A∨B |
| NOT | ¬ | A | ¬A |
| XOR | ⊕ | A B | A⊕B |
Library routines live inside a module (Python) or package (Java). They must be imported before use.
import math – use as math.sqrt(...)from math import sqrt, pi – use directly as sqrt(...)import math as m – use as m.sqrt(...)Exam tip: Avoid import * – it obscures the namespace and can cause name clashes.
math and random# math – area of a circleimport math
r = 5
area = math.pi * math.pow(r, 2)
print("Area =", area)
# random – roll a die
import random
die = random.randint(1, 6) # inclusive
print("Die roll:", die)
# Write numbers to a filewith open('numbers.txt', 'w') as f:
for n in [12, 7, 5]:
f.write(str(n) + '\n')
# Read back and compute the mean
total = 0
count = 0
with open('numbers.txt', 'r') as f:
for line in f:
total += int(line.strip())
count += 1
mean = total / count
print('Mean =', mean)
try:f = open('data.txt', 'r')
data = f.read()
except FileNotFoundError:
print('File not found – please check the filename.')
finally:
f.close()
| Language | Try‑Catch Syntax |
|---|---|
| Java | try { |
| Visual Basic | Try |
| Structure | Python | Pseudocode (Cambridge) | Java | Visual Basic |
|---|---|---|---|---|
| Count‑controlled loop | for i in range(start, stop, step): | FOR i ← start TO stop‑1 STEP step | for (int i = start; i < stop; i += step) { … } | For i As Integer = start To stop - 1 Step step |
| Pre‑condition loop | while condition: | WHILE condition DO | while (condition) { … } | Do While condition |
| Post‑condition loop | while True: | REPEAT … UNTIL NOT condition | do { … } while (condition); | Do … Loop While condition |
| IF … ELSE IF … ELSE | if cond1: | IF cond1 THEN … | if (cond1) { … } else if (cond2) { … } else { … } | If cond1 Then … ElseIf cond2 Then … Else … End If |
| CASE (select) statement | match expr: | CASE expr OF | switch (expr) { case 1: … break; … default: … } | Select Case expr |
When choosing a loop or data‑structure, note its time‑complexity:
In exam answers, a brief comment such as “linear time, O(n)” earns marks for justification.
None in Python).return.# Procedure – prints a greetingdef greet(name):
print("Hello, " + name)
# Function – factorial (recursive)
def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)
# Simple stack ADT using a list
class Stack:
def init(self):
self._data = [] # private list
def push(self, item):
self._data.append(item)
def pop(self):
if not self._data:
raise IndexError("pop from empty stack")
return self._data.pop()
def is_empty(self):
return len(self._data) == 0
PROCEDURE Greet(name)OUTPUT "Hello, " + name
END PROCEDURE
FUNCTION Factorial(n) RETURNS INTEGER
IF n = 0 THEN
RETURN 1
ELSE
RETURN n * Factorial(n-1)
END IF
END FUNCTION
TYPE Stack
data : ARRAY[0..99] OF INTEGER // simple fixed‑size array
top : INTEGER := -1
END TYPE
PROCEDURE Push(s : INOUT Stack, value : INTEGER)
s.top ← s.top + 1
s.data[s.top] ← value
END PROCEDURE
FUNCTION Pop(s : INOUT Stack) RETURNS INTEGER
IF s.top = -1 THEN
ERROR "Stack underflow"
END IF
value ← s.data[s.top]
s.top ← s.top - 1
RETURN value
END FUNCTION
| Language | Procedure (void) | Function (returns value) | ADT (stack) |
|---|---|---|---|
| Java | public static void greet(String name) { | public static int factorial(int n) { | public class Stack { |
| Visual Basic | Sub Greet(name As String) | Function Factorial(n As Integer) As Integer | Structure Stack |
| Concept | Python | Java | Visual Basic | Pseudocode (Cambridge) |
|---|---|---|---|---|
| Array (fixed size) | arr = [0]*10 # list used as array | int[] arr = new int[10]; | Dim arr(9) As Integer | ARRAY arr[0..9] OF INTEGER |
| Record / Structure | class Person: pass # simple class | class Person { String name; int age; } | Structure Person | RECORD Person |
| File (sequential text) | with open('file.txt','r') as f: … | BufferedReader br = new BufferedReader(new FileReader("file.txt")); | Dim sr As New StreamReader("file.txt") | OPEN "file.txt" FOR INPUT AS #1 |
| Stack ADT | list.append(x) / list.pop() | java.util.Stack<Integer> s = new Stack<>(); | Dim s As New Stack(Of Integer) | PROCEDURE Push(s : INOUT Stack, v : INTEGER) |
| Queue ADT | from collections import deque; q = deque(); q.append(x); q.popleft() | java.util.Queue<Integer> q = new LinkedList<>(); | Dim q As New Queue(Of Integer) | PROCEDURE Enqueue(q : INOUT Queue, v : INTEGER) |
Key points for exams
# TODO: add input validation for security demonstrates awareness.| Aspect | Built‑in Function | Library Routine |
|---|---|---|
| Availability | Always available; no import required. | Must import the module/package first. |
| Typical Use | Basic I/O, type conversion, simple sequence operations, elementary maths (e.g., abs, pow). | Specialised tasks – advanced maths, random numbers, file I/O, system interaction, date‑time handling, data structures. |
| Namespace | Global namespace. | Accessed via module.function or from module import …. |
| Performance | Very fast – no module lookup. | Slight overhead for import and lookup, negligible for exam‑level programs. |
| Documentation | Language reference. | Module API guide (e.g., math). |
import *.from math import sqrt, pi) when you need only a few routines.try/except or try/catch).sum() and len() (built‑ins).math.pow(x, y) and the exponentiation operator x**y. Include a short example.random module, justify your choice and show the code.saveData that writes a list of strings to a file called output.txt. Show both the Python implementation and the equivalent pseudocode.try/except block that attempts to open a file called data.txt. If the file does not exist, output “File not found”. Include a finally clause that closes the file.push, pop and isEmpty operations. Provide Python code and the corresponding Cambridge pseudocode.Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.