September 2026
1051 pages
35h 4m
English
A loop allows you to execute a block of code, the loop body, several times in a row. Python distinguishes between two types of loops: the while loop as a simple loop structure and the for loop for running through more complex data types.
We already used the while loop in the number-guessing game. It’s used to execute a block of code as long as a certain condition is met. In our first program in Chapter 4, Section 4.3, a while loop was used to read a new number from the player until the number entered matched the number we were looking for.
Basically, a while loop consists of a loop header containing the condition and a loop body corresponding to the code block to be executed (note that the loop runs as long ...
Read now
Unlock full access