Control Flow

For Loops

for iterates over values produced by an iterable.

Direct iteration keeps the code focused on the values in the collection.

Source

for name in ["Ada", "Grace", "Guido"]:
    print(name)

Output

Ada
Grace
Guido

range(3) yields 0, 1, and 2 lazily. Use it when those integers are the thing being iterated over.

Source

for number in range(3):
    print(number)

Output

0
1
2
abcdnext()abcdnext()abcdnext()abcdnext() — last
Each call to next() advances the caret one cell along the iterable — the same shape behind range(), strings, and any sequence.

enumerate() is the usual Python way to keep a counter beside each value without indexing back into the list.

Source

for index, name in enumerate(["Ada", "Grace"], start=1):
    print(index, name)

Output

1 Ada
2 Grace

Notes

See also

Run the complete example

Example code

Expected output

Ada
Grace
Guido
0
1
2
1 Ada
2 Grace

Execution time appears here after you run the example.