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 → Binary
bin(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 0 0 | 0 0 1 | 0 1 0 | 0 1 1 | 1 |
| OR | ∨ | A B | A∨B 0 0 | 0 0 1 | 1 1 0 | 1 1 1 | 1 |
| NOT | ¬ | A | ¬A 0 | 1 1 | 0 |
| XOR | ⊕ | A B | A⊕B 0 0 | 0 0 1 | 1 1 0 | 1 1 1 | 0 |
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 circle
import 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 file
with open('numbers.txt', 'w') as f:
for n in [12, 7, 5]:
f.write(str(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 {
// code that may throw an exception
} catch (FileNotFoundException e) {
System.out.println("File not found");
} finally {
// optional clean‑up code
} |
| Visual Basic | Try
' code that may raise an error
Catch ex As IOException
Console.WriteLine("File not found")
Finally
' clean‑up code
End 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:
…
if not condition: break |
REPEAT … UNTIL NOT condition |
do { … } while (condition); |
Do … Loop While condition |
| IF … ELSE IF … ELSE | if cond1:
…
elif cond2:
…
else:
… |
IF cond1 THEN …
ELSE IF cond2 THEN …
ELSE …
ENDIF |
if (cond1) { … } else if (cond2) { … } else { … } |
If cond1 Then … ElseIf cond2 Then … Else … End If |
| CASE (select) statement | match expr:
case 1: …
case 2: …
case _: … |
CASE expr OF
1: …
2: …
OTHERWISE … |
switch (expr) { case 1: … break; … default: … } |
Select Case expr
Case 1
…
Case 2
…
Case Else
…
End Select |
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 greeting
def 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) {
System.out.println("Hello, " + name);
} |
public static int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n-1);
} |
public class Stack {
private int[] data = new int[100];
private int top = -1;
public void push(int v) {
data[++top] = v;
}
public int pop() {
if (top < 0) throw new EmptyStackException();
return data[top--];
}
public boolean isEmpty() {
return top == -1;
}
} |
| Visual Basic | Sub Greet(name As String)
Console.WriteLine("Hello, " & name)
End Sub |
Function Factorial(n As Integer) As Integer
If n = 0 Then
Return 1
Else
Return n * Factorial(n - 1)
End If
End Function |
Structure Stack
Private data(99) As Integer
Private top As Integer = -1
End Structure
Sub Push(ByRef s As Stack, ByVal v As Integer)
s.top += 1
s.data(s.top) = v
End Sub
Function Pop(ByRef s As Stack) As Integer
If s.top = -1 Then Throw New InvalidOperationException("underflow")
Dim v As Integer = s.data(s.top)
s.top -= 1
Return v
End Function |
| 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
name As String
age As Integer
End Structure |
RECORD Person
name : STRING
age : INTEGER
END RECORD |
| 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
…
CLOSE #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) |
| 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.Create an account or Login to take a Quiz
Log in to suggest improvements to this note.
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.