Home / Python / Control flow

Loops

for repeats code for each item in a sequence, such as a list. while repeats as long as a condition is true. Wrap a repeated task in a function to reuse it.

for n in [1, 2, 3]:
    print(n * 2)

i = 0
while i < 3:
    i += 1

Test yourself on this