For and While Loops

Loops let you repeat code. Python supports for and while loops.

For Loop

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

range(n) generates numbers from 0 to n-1.

While Loop

count = 0
while count < 5:
  print(count)
  count += 1

Be careful with while loops to avoid infinite loops.

← PrevNext →