Skip to content
IGCSE·Tuition
Computer Science · Lessons

Map an algorithm into a restricted Python function

You can read the pseudocode fluently, then freeze when asked to say the same thing in Python.

On this page
  1. How does each pseudocode piece map to Python?
  2. Worked example
  3. The mistake to watch for
  4. Check yourself
  5. Where this leads next

To map an algorithm into Python, translate it one structure at a time: assignment, selection, loop, output. Keep the logic exactly the same and change only the notation.

This is the first lesson in Python transition and execution. It builds on the notation from pseudocode for the applicable syllabus.

How does each pseudocode piece map to Python?

IdeaCambridge-style pseudocodePython
Store a valueCount ← 0count = 0
Compare for equalityIF Mark = 50if mark == 50:
Other comparisons>=, <=, <>>=, <=, !=
Count-controlled loopFOR Index ← 1 TO 5for index in range(1, 6):
Loop over a listFOR with Scores[Index]for score in scores:
End of blockENDIF, NEXTindentation
Show a resultOUTPUT Countprint(count) or return count

The range function stops one short of its second number. To count 1 to 5 you write range(1, 6).

Worked example

Algorithm: count how many of five marks are 50 or more.

Count ← 0
FOR Index ← 1 TO 5
   IF Scores[Index] >= 50 THEN
      Count ← Count + 1
   ENDIF
NEXT Index
OUTPUT Count

Step 1, wrap it in a function. The array becomes a parameter, so the function can be tested with any list.

Step 2, translate each structure.

def count_passes(scores):
    count = 0
    for score in scores:
        if score >= 50:
            count = count + 1
    return count

print(count_passes([72, 45, 50, 38, 91]))

Step 3, trace with 72, 45, 50, 38, 91.

scorescore >= 50count after
(start)0
72True1
45False1
50True2
38False2
91True3

The output is 3. Notice that 50 counts, because the test is >= and not >.

The mistake to watch for

A frequent slip is to copy the pseudocode index range straight into Python.

Mistaken answer: for index in range(1, len(scores)): then scores[index]

This starts at position 1, but Python lists start at position 0, so the first mark is skipped. With the list above it sees 45, 50, 38, 91 and returns 2.

The correction is to loop over the values directly, as in for score in scores, which cannot skip or overrun. If you need an index, range(len(scores)) runs from 0 to the last position. Always trace the first and last item to catch this.

Check yourself

1. Write the Python for Total ← Total + Mark.

Show answer

total = total + mark. The arrow becomes =, and the variable names are lower case by habit.

2. Write the Python condition for IF Mark = 100 THEN.

Show answer

if mark == 100:. Two equals signs compare, and the line ends with a colon.

3. What does count_passes([50, 50, 49]) return?

Show answer

2. The two 50s pass because 50 >= 50 is True, and 49 does not.

Where this leads next

Next, see how spacing changes behaviour in testing indentation and branch structure. Try the function above in the Python reasoning sandbox and compare it with the restricted pseudocode trace trainer.

If you translate correctly but lose marks on the trace, our online one-to-one Computer Science tuition can focus on exactly that habit.

Questions people ask

Do I have to answer in Python in my exam?

Follow the instruction in each question and the syllabus for your exam year. If a question asks for pseudocode, write pseudocode. Check the Computer Science 0478 syllabus page on the Cambridge website, and ask your exam centre if your syllabus version is unclear.

What is the difference between print and return?

print shows a value on the screen and the function carries on. return hands a value back to whatever called the function and ends the function. If a question says a function should give back a result, use return, then print the returned value when you test it.

Why does Python not need ENDIF or NEXT?

Python marks the end of a block by indentation. Lines indented under an if or for belong to it, and the first line indented less ends the block. That is why consistent spacing matters so much in Python code.

Updated:

Your next step

If translating line by line still feels like guessing, a one-to-one teacher can build a mapping table with you and check each line against a trace.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80. Other fees, schedules and ongoing arrangements are confirmed directly with your teacher after the trial class.

Tuition is arranged with a parent or guardian. Send them this page on WhatsApp and they can enquire for you.

Parent or guardian? Enquire here

9,000+ students helped through our service