Skip to content
ExamSpine

Lesson 5.11 · Trace it

Trace: swap pass

Python code7 rows to fill3 marks

Complete the trace table for this Python code. The line values[i], values[i + 1] = values[i + 1], values[i] swaps the two values in one step. Then answer the question below the table. [3]

Line 1: values = [3, 1, 4, 2]
Line 2: for i in range(len(values) - 1):
Line 3, indented 1 level: if values[i] > values[i + 1]:
Line 4, indented 2 levels: values[i], values[i + 1] = values[i + 1], values[i]
Line 5: print(values)

Fill in one row each time a line changes i or values, or prints something. The table already has the right number of rows. In each row, write only what that line changes; you can leave the other cells empty or copy their values down. Write values the way Python shows them: 3 and 3.0 are different, and text goes in quotes. In the output column, write exactly what appears on screen, with no quotes around it.

Rowivaluesoutput
1
2
3
4
5
6
7