Loops and Other Compound Statements

In the previous chapter, we learned how to create and operate on data structures. Now, let's discuss how to operate on them effectively.

We will first cover loops—a special type of compound statement (code that compounds other code, just like functions)—that allows the same code to be run over and over—any number of times, or even indefinitely. After loops, we will discuss if-else statements—logical forks that allow us to split code execution based on test results. Finally, we will cover two less popular, but still very useful, clauses—try, which helps to save the day if something goes wrong (an error is raised) within the code, and with , which helps to close the context safely (for example, close the ...

Get Learn Python by Building Data Science Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.