CHAPTER 7
Repeating Actions with Loops
In this chapter, you start using Python’s loops to repeat actions as needed in your scripts. You learn to create both for loops and while loops, use loop control statements, and nest loops within each other to implement complex repetition.

Understanding How for Loops Work
Understanding How while Loops Work
Understanding break Statements in Loops
Using a break Statement to Exit a Loop Early
Understanding continue Statements in Loops
Using a continue Statement in a Loop
Understanding else Statements in Loops
Understanding Python’s Loops
When you need to repeat actions in your code, you can use Python’s loop structures. A for loop lets you iterate — repeat — actions either once for each object in a collection, such as once for each letter in a string of text, or a specific number of times, such as 10 times. A while loop enables you to repeat actions as long as a condition remains True — for example, while a value is above a specified cutoff. When you need more complex repetition, you can nest either type of loop or a mixture of the two types.
Using for Loops for Definite Iteration
A for loop enables you to repeat actions for a predetermined number of times. This type of repetition ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access