CHAPTER 5

Execution Control Structures

5.1 Decision Control and the if Statement

5.2 for Loop and Iteration Patterns

5.3 More on Lists: Two-Dimensional Lists

5.4 while Loop

5.5 More Loop Patterns

5.6 Additional Iteration Control Statements

Chapter Summary

Solutions to Practice Problems

Exercises

Problems

THIS CHAPTER COVERS, in more depth, the Python statements and techniques that provide control over what code blocks will be executed when, and how often.

We start the discussion with the Python decision control structure, the if statement. The if statement was introduced in Chapter 3 in its one-way and two-way formats. We introduce here the general format: a multiway decision control structure that allows an arbitrary number of conditions and associated alternative code blocks to be defined.

We provide next an in-depth coverage of the Python iteration control structures and techniques. Two Python statements provide the ability to execute a block of code repeatedly: the for loop and the while loop. Both are used in many different ways. The bulk of this chapter is spent on the different iteration patterns, and when and how to use them.

Understanding different iteration patterns is really about understanding different approaches to breaking up problems and solving them iteratively. This chapter is thus fundamentally about problem solving.

5.1 Decision Control and the if Statement

The if statement is the fundamental decision control structure that enables alternative code blocks to ...

Get Introduction to Computing Using Python: An Application Development Focus 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.