Pseudocode concepts have direct equivalents in high-level programming languages like Python. Understanding this correspondence allows you to both write algorithms in pseudocode and implement them in code.
KEY TAKEAWAY: Every pseudocode construct has a direct programming language equivalent. VCE Algorithmics uses Python as the reference language for implementation.
| Pseudocode | Python Equivalent |
|---|---|
x ← 5 |
x = 5 |
if ... then ... else ... end if |
if ...: ... else: ... |
for i ← 0 to n do |
for i in range(n+1): |
while condition do |
while condition: |
FUNCTION f(x) |
def f(x): |
return value |
return value |
A[i] (array index) |
A[i] |
length(A) |
len(A) |
AND, OR, NOT |
and, or, not |
mod |
% |
// comment |
# comment |
# Pseudocode:
x ← 10
y ← x * 2
# Python:
x = 10
y = x * 2
# Pseudocode:
if temperature > 30 then
print("Hot")
else if temperature > 20 then
print("Warm")
else
print("Cool")
end if
# Python:
if temperature > 30:
print("Hot")
elif temperature > 20:
print("Warm")
else:
print("Cool")
# Pseudocode:
for i ← 1 to 5 do
print(i)
end for
# Python:
for i in range(1, 6):
print(i)
# Pseudocode:
while n > 0 do
n ← n - 1
end while
# Python:
while n > 0:
n -= 1
# Pseudocode:
FUNCTION factorial(n)
if n = 0 then
return 1
else
return n * factorial(n - 1)
end if
# Python:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
Python’s built-in types correspond to many ADTs:
| ADT | Python Type/Module |
|---|---|
| Array / List | list |
| Dictionary | dict |
| Set | set |
| Stack | list (use append/pop) |
| Queue | collections.deque |
| Priority Queue | heapq module |
| Graph | Custom class or dict of list |
# Stack using Python list
stack = []
stack.append(5) # push
stack.append(3) # push
top = stack[-1] # peek
stack.pop() # pop
# Queue using deque
from collections import deque
queue = deque()
queue.append('A') # enqueue
queue.popleft() # dequeue
EXAM TIP: In VCAA exams, you may be asked to implement pseudocode in Python. Ensure you understand that
range(a, b)gives integers fromatob-1(notb). Userange(0, n)to correspond tofor i ← 0 to n-1.COMMON MISTAKE: Python uses
==for equality comparison, but pseudocode uses=. Remember this distinction when reading or translating between the two.