Loops
A for loop walks through a sequence. range(5) gives 0, 1, 2, 3, 4. A while loop repeats while its condition is true.
Example
for i in range(3):
print("Number", i)
n = 3
while n > 0:
print("Countdown", n)
n -= 1Result
Number 0 Number 1 Number 2 Countdown 3 Countdown 2 Countdown 1
Exercise
Loop over the numbers 0 to 4.
for i in (5): print(i)
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.