Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

In this topic students learn to repeat lines of code a set number of times, using for loops and range.

Learning objectives and goals

Be able to read, comprehend, trace, adapt and create Python code that:

Slide deck

This chapter has no slide deck. It was written for Dodona from new material by the course author, after the rest of the course had been converted.

Teacher notes

This chapter is the second half of iteration. The previous chapter covers while loops, where the program repeats until something changes; this one covers for loops, where the number of repetitions is known before the loop starts.

The exam specification calls these count controlled and condition controlled iteration, and both activities and exercises use those terms alongside for and while. It is worth being explicit with students that this is one idea with two shapes, and that the question to ask of any repeated task is “do I know how many times?”.

The pedagogical argument for teaching while first, as this course does, is that a while loop makes every part of the mechanism visible: the counter is created, compared and changed in three separate places, and forgetting the third gives an infinite loop. A for loop does all three at once. Students who have seen the long form first tend to read for counter in range(10): as the short form of something they already understand, rather than as new magic.

Key points

Errors and misconceptions