for and while loops

Loops repeat a block of statements. for iterates over the elements of an iterable (list, range, string), while while repeats as long as a condition remains true. break interrupts the loop and continue moves on to the next iteration. A while condition that never becomes false causes an infinite loop.

Example

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

Related concepts

The whole glossary