Python Fundamentals ยท Control Flow
Repeat once per item: numbers from range, counting down with a step, and getting both position and item with enumerate.
total = 0
for n in range(1, 5): # 1, 2, 3, 4: the end is not included
total = total + n
for i in range(10, 0, -4): # start, stop, step: counts down
print(i)
for index, colour in enumerate(["red", "blue"]):
print(index, colour)
print(total)total is created with the value 0.n changes from 1 to 2.total changes from 1 to 3.n changes from 2 to 3.total changes from 3 to 6.n changes from 3 to 4.for n in range(1, 5): # 1, 2, 3, 4: the end is not included.10.i changes from 10 to 6.6.i changes from 6 to 2.2.for i in range(10, 0, -4): # start, stop, step: counts down.0 red.index changes from 0 to 1. colour changes from 'red' to 'blue'.1 blue.for index, colour in enumerate(["red", "blue"]).