Skip to content
IGCSE·Tuition
Computer Science · Practice

Python transition and execution: mixed practice

Translating feels easy when the answer is on the page, and harder when the screen is blank.

This set covers the whole of Python transition and execution: mapping pseudocode into Python, indentation and branches, kinds of error, the safe runtime and exam-year labels. Questions run from easier to harder, and all are original.

Trace each one on paper before opening the answer. Afterwards, check with the Python reasoning sandbox and the restricted pseudocode trace trainer. The mistake log and retest queue is a good place to record anything you got wrong.

Questions

1. Write the Python for these two lines.

Total ← 0
Total ← Total + 5
Show answer
total = 0
total = total + 5

The arrow ← becomes =. After both lines, total holds 5.

2. Write Python for: IF Age >= 16 THEN OUTPUT "Senior" ENDIF.

Show answer
if age >= 16:
    print("Senior")

The condition ends with a colon, and the print line is indented. The text in quotes stays exactly the same.

3. What is output?

def f(n):
    total = 0
    for i in range(1, n + 1):
        total = total + i
    return total

print(f(4))
Show answer

range(1, 5) gives 1, 2, 3, 4. Total goes 1, 3, 6, 10. Output: 10.

4. What is output?

for i in range(3):
    print(i * 2)
Show answer

range(3) gives 0, 1, 2, so the values printed are 0, 2 and 4, on three lines. Output: 0, 2, 4.

5. Give the result of band(70), band(40) and band(39).

def band(mark):
    if mark >= 70:
        return "High"
    elif mark >= 40:
        return "Middle"
    else:
        return "Low"
Show answer

70 >= 70 is True, so High. 40 fails the first test, passes >= 40, so Middle. 39 fails both, so Low.

6. Find the syntax error.

def double(x)
    return x * 2
Show answer

The def line is missing its colon. Corrected: def double(x):. Python cannot start the program until this is fixed.

7. This function should give the average of two numbers. What does average(4, 10) return, what should it return, and what kind of error is it?

def average(a, b):
    return a + b / 2
Show answer

Division comes first: 10 / 2 = 5, then 4 + 5 = 9.0. The correct average is (4 + 10) / 2 = 7.0. It is a logic error, because it runs without a message. Fix: return (a + b) / 2.

8. What is output, and what would the output be if count = count + 1 were moved back to line up with if?

count = 0
for n in [3, 8, 5, 10]:
    if n > 4:
        count = count + 1
print(count)
Show answer

As written, 8, 5 and 10 are greater than 4, so the output is 3. Moved back to the level of if, the line runs for every item, so the output becomes 4.

9. What is output? Then say what happens if the line n = n * 2 is removed.

n = 1
while n < 20:
    n = n * 2
print(n)
Show answer

n goes 1, 2, 4, 8, 16, 32. At 32 the test 32 < 20 is False, so the output is 32. Without n = n * 2, n never changes, the test stays True and the loop never ends. A runtime with a time limit stops it.

10. A question says “write the algorithm in pseudocode”. You usually code in Python. What should you write, and where would you check which notation your exam year expects?

Show answer

Write pseudocode in Cambridge style, because the question asks for it. Check the syllabus for your own exam year on the Cambridge IGCSE Computer Science 0478 page, and ask your exam centre which syllabus you are entered for. Advice written for another year is unchecked until confirmed.

11. This does not run in the browser sandbox. Why, and how do you fix the exercise?

marks = open("marks.txt").read()
Show answer

The runtime has no file access. Put invented data in the program: marks = [72, 45, 50, 38, 91]. The algorithm you are testing stays the same.

If you got these wrong

What went wrongGo to
Translation, range ends, = versus == (Q1 to Q4)Map an algorithm into a restricted Python function
Branch order, indentation, loop placement (Q5, Q8)Test indentation and branch structure
Error types and wrong results (Q6, Q7)Distinguish a syntax error from a wrong result
Endless loops, files, sandbox limits (Q9, Q11)Use a safe browser runtime
Notation and exam year (Q10)Keep 2029 Python-only guidance separate

For help turning one wrong habit into a reliable one, see our online one-to-one Computer Science tuition.

Updated:

Your next step

If the same kind of question keeps going wrong, a one-to-one teacher can look at your traces and find the habit behind the slip.

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