For Loop (Python)

A for loop is used to iterate over a list or other sequence.

The for loop is a fundamental building block of Python programming. Let’s say we we have values stored in a list (notes, intervals, rhythms, etc.) and we want to step through or iterate through them in order to display them or perform some operation on them individually. We can use a for loop to structure this operation.

First we begin with a list. In this example, we’ll use a list of numbers (actually the MIDI note numbers corresponding to the C major scale):

my_list = [60, 62, 64, 65, 67, 69, 71, 72]

We can iterate over each element in the list with a for loop:

for x in my_list:
 print(x)

Each command are nested within the for statement is executed for each value in the range specified. In this case, each value in the list my_list will be printed:

> 60
> 62
> 64
> 65
> 67
> 69
> 71
> 72

For more, see the official Python tutorial, or the w3schools page.