Loops in Python
Chapter 7: Loops
7 short lessons, with 3D animations, quizzes and coding tasks.
The lessons
while loops
while repeats its block as long as its condition is True, checking the condition again before every lap.
for and range()
for takes each value from a sequence, like range(4), puts it in the loop variable, and runs the block once for each value.
Looping over text, and keeping a total
A for loop can walk through a string one character at a time, and an accumulator is a variable made before the loop that collects the answer inside it.
break and continue
continue skips the rest of this lap, and break ends the whole loop at once.
Loops inside loops
In a nested loop, the inner loop runs all the way through, from start to finish, for every lap of the outer loop.
A loop's else, and the walrus operator :=
A for or while loop can have an else: block, which runs when the loop ends by itself (it runs out of values, or its condition becomes False), and is skipped when break ends it.
Debugging: finding and fixing mistakes
Debugging means finding the exact place where your program does something different from what you expected, by reading the error, looking at the values, and checking one step at a time.