PyPad

Python for loops: repeat code for each item

A for loop runs the same lines of code once for each item in a sequence, so you write the instruction once instead of many times.

Open in PyPad to change it and keep your own copy.

How it works

Each time round the loop, the name after for holds the next item, and the indented lines below it run. The loop stops when the items run out.

Try changing it

A common mistake

Forgetting the indentation, or the colon at the end of the for line, gives an error. Python needs both to know which lines to repeat.