4.3. The for Loop

When we drew the square, it was quite tedious. We had to move then turn, move then turn, etc. etc. four times. If we were drawing a hexagon, or an octogon, or a polygon with 42 sides, it would have been a nightmare to duplicate all that code.

A basic building block of all programs is to be able to repeat some code over and over again. In computer science, we refer to this repetitive idea as iteration. In this section, we will explore some mechanisms for basic iteration.

In Python, the for statement allows us to write programs that implement iteration. As a simple example, let’s write a program that prints a greating multiple times.

Take a look at the output produced when you press the run button. Here’s how it works:

You have attempted of activities on this page
4.2. Our First Turtle Program"> 4.4. Flow of Execution of the for Loop">Next Section - 4.4. Flow of Execution of the for Loop