Algorithmic Thinking & Problem Solving~20 min

Pseudocode and Tracing

Write an algorithm in plain language before any Python exists, and trace it by hand to catch mistakes while they're still cheap to fix.

By the end of this lesson, you can

  • Write language-neutral pseudocode for a small algorithm before implementing it in Python
  • Trace pseudocode by hand to verify it's correct before writing any code
  • Translate pseudocode into a working Python implementation

Why it matters

It’s tempting to jump straight from “I understand the problem” to typing Python — but syntax mistakes and design mistakes are easy to tangle together when you do both at once. Pseudocode lets you work out whether an approach is correct using plain language, with none of Python’s grammar to get in the way. Tracing it by hand — before any code exists — catches a wrong algorithm while it’s still just a few lines to fix, not a program to debug.

Mental model

An algorithm is the plan for solving a problem — precise enough to follow step by step, but not tied to any one language. Pseudocode is how that plan gets written down. Take the classic problem of finding the largest number in a list:

PseudocodeAn algorithm for finding the biggest number
set biggest to the first number
for each remaining number in the list:
    if the number is bigger than biggest:
        set biggest to the number
show biggest

No def, no for ... in, no if syntax — just the idea, precise enough that a person could carry it out with a pencil and a list of numbers.

Trace it — before any code exists

Tracing pseudocode works exactly like tracing a program: follow it step by step, writing down how biggest changes, for the list [3, 7, 2, 9, 4]:

Tracing the pseudocode by hand, before writing any Python
StepnumberIs number > biggest?biggest
start(none)(none)3
17yes7
22no7
39yes9
44no9

The trace ends with biggest = 9 — correct. Working this out on paper, before writing a single line of Python, is what makes pseudocode useful: a wrong algorithm shows up here, in a table, rather than as a confusing bug in running code later.

Translate it into Python

Once pseudocode has been traced and trusted, translating it is close to mechanical — each pseudocode line maps to roughly one Python line:

PythonThe traced algorithm, now in Python
def find_biggest(numbers):
    biggest = numbers[0]
    for number in numbers:
        if number > biggest:
            biggest = number
    return biggest

print(find_biggest([3, 7, 2, 9, 4]))
Output
9

Same result the hand trace predicted — which is the point: the trace already proved this was correct before this code was ever run.

Check your understanding

Trace this pseudocode for the list [5, 1, 8, 3]. What does it end with? set smallest to the first number for each remaining number in the list: if the number is smaller than smallest: set smallest to the number show smallest
Or reveal the answer without checking

Answer:1
Starting at 5: 1 is smaller (smallest becomes 1), 8 is not smaller, 3 is not smaller than 1. The trace ends with smallest = 1, the actual minimum of the list.

What's the main benefit of tracing pseudocode before writing any Python?
Or reveal the answer without checking

Answer:It catches a wrong algorithm while fixing it only means changing a few lines of plain language, not debugging running code
A trace either confirms the approach works or reveals it doesn't, before any Python exists — so a design mistake is caught and fixed at its cheapest possible point.

Practice: warm-up

Trace this pseudocode by hand for the list [4, 4, 4], then for the empty list [], before revealing the answer.

set count to 0
for each number in the list:
    if the number equals 4:
        set count to count + 1
show count

Stuck? Reveal one hint at a time.

  1. Hint 1

    For [4, 4, 4], count starts at 0 and increases once per matching number.

  2. Hint 2

    For the empty list, the for loop body never runs at all — what does that leave count as?

Reveal both traces

Try the problem yourself before reading this. There is often more than one reasonable approach — treat this as one worked example, not the only correct answer.

[4, 4, 4]: count = 0 -> 1 -> 2 -> 3.  Final: count = 3
[]:        the loop body never runs.  Final: count = 0 (its starting value)

Practice: apply it

Translate this pseudocode into a Python function called count_negatives:

set count to 0
for each number in the list:
    if the number is less than 0:
        set count to count + 1
show count
Which Python line correctly checks "the number is less than 0"?
Or reveal the answer without checking

Answer:if number < 0:
< is Python's less-than comparison. if number = 0: uses assignment (=) instead of comparison, which isn't valid inside a condition. if 0 < number: checks the opposite thing — whether number is greater than 0.

Modification challenge: write the complete count_negatives(numbers) function, then trace it by hand against [-2, 5, -1, 0] to confirm it returns 2 before running it.

Summary

  • Pseudocode describes an algorithm in plain, language-neutral language, so it can be checked before any Python is written.
  • Tracing pseudocode by hand — the same skill used to trace real code — catches a wrong algorithm while it’s still cheap to fix.
  • Translating traced, trusted pseudocode into Python is close to mechanical: syntax and design mistakes stay separated instead of tangled together.
  • Writing pseudocode after the code already exists can’t catch design mistakes — it only restates whatever is already there.