Skip to content

Module 18 · Algorithms and Problem Solving — topic 4 of 18

Pseudocode

Describing algorithms in structured plain language: common pseudocode keywords, indentation for structure, writing pseudocode before code, and translating it line by line into Python.

Beginner 8 min read·Lesson 264 of 306 in Python Programming

Learning objectives

  • Write clear pseudocode for an algorithm
  • Translate pseudocode into Python
  • Use pseudocode to plan before coding

Definition

Pseudocode is an informal, language-independent description of an algorithm that uses structured English with programming-like keywords (IF, WHILE, FOR EACH, RETURN) and indentation, without worrying about exact syntax.

Introduction

Pseudocode is how programmers sketch algorithms on whiteboards: precise enough to implement, loose enough to write quickly.

Why it matters

Separating 'what are the steps?' from 'what is the syntax?' makes hard problems easier and lets you discuss algorithms with anyone.

Detailed explanation

Pseudocode has no strict rules, but common conventions use keywords such as SET, READ, PRINT, IF ... THEN ... ELSE, WHILE, FOR EACH, RETURN and END.

Indentation shows which steps belong inside a loop or decision, just like in Python.

Write at the level of ideas: 'find the largest value in the list' is fine if it's obvious; break it down further when it isn't.

Good pseudocode is precise about data and conditions — 'IF count is 0' rather than 'if there's nothing'.

Python's readable syntax makes translation very direct: FOR EACH becomes for, SET x TO 0 becomes x = 0.

Pseudocode is also useful as comments: write the steps as comments, then fill in code under each.

Exam boards and textbooks often define their own pseudocode style; the ideas are the same.

Check pseudocode by tracing it with a small example before writing any code.

Syntax

Text
FUNCTION average_positive(numbers)
SET total TO 0
SET count TO 0
FOR EACH n IN numbers
IF n > 0 THEN
ADD n TO total
ADD 1 TO count
END IF
END FOR
IF count = 0 THEN RETURN 0
RETURN total / count
END FUNCTION
  • Structured English with keywords.
  • Indentation shows structure.
  • Translate line by line.

Code examples

Pseudocode for a search

Text
FUNCTION contains(items, target)
FOR EACH item IN items
IF item = target THEN
RETURN TRUE
END IF
END FOR
RETURN FALSE
END FUNCTION

Translating to Python

Python
def average_positive(nums):
total = 0
count = 0
for n in nums:
if n > 0:
total += n
count += 1
if count == 0:
return 0
return total / count
print(average_positive([4, -2, 6, 0, 5]))
Output
5.0

More examples

Pseudocode as comments

Python
def longest_word(sentence):
# SET best TO empty text
best = ""
# FOR EACH word IN sentence
for word in sentence.split():
# REMOVE punctuation from the ends of word
word = word.strip(".,!?")
# IF word is longer than best THEN SET best TO word
if len(word) > len(best):
best = word
# RETURN best
return best
print(longest_word("Python makes problem solving enjoyable!"))
print(repr(longest_word("")))
Output
enjoyable
''

Step-by-step explanation

  • Each comment is one line of the original pseudocode.
  • The Python under each comment implements that step directly.
  • Tracing the empty sentence shows the loop never runs and the empty string is returned.

Expected output

Running “Pseudocode as comments” prints exactly:

TextOutput
enjoyable
''

Practical applications

  • Whiteboard interviews and design discussions.
  • Algorithm descriptions in textbooks and research papers.

Best practices

  • Write pseudocode before tricky code.
  • Be precise about conditions and data.
  • Trace it with an example.

Common mistakes

Pseudocode that is really code

Avoid

for (int i = 0; i < n; i++) { ... }

Prefer

FOR EACH item IN list

Pseudocode should express the idea, not a particular language's syntax.

Vague steps

Avoid

process the data

Prefer

FOR EACH row: ADD row.amount TO total

Steps must be specific enough to implement.

Important notes

  • Many algorithm books present algorithms only in pseudocode so readers can use any language.

Key takeaways

  • Pseudocode describes algorithms in structured English.
  • Indentation shows structure.
  • It translates directly into Python.

Try it yourself

  1. 1
    Write pseudocode for FizzBuzz, then translate it to Python.
  2. 2
    Write pseudocode for counting vowels in a sentence and trace it with "Hello World".

Finished reading? Mark it complete to update your dashboard.

Suggested practice

16 questions cover the “Algorithms and Problem Solving” module. Answers and explanations are revealed after you submit.

  • AdvancedWhat is printed?
  • AdvancedWhat is the average time complexity of x in my_set?
  • AdvancedA loop over a list checks item in other_list for each item. With both lists of length n, the complexity is…
Practise this module