O'Reilly logo

The Quick Python Book by Naomi R. Ceder

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 8. Control flow

This chapter covers

  • Repeating code with a while loop
  • Making decisions: the if-elif-else statement
  • Iterating over a list with a for loop
  • Using list and dictionary comprehensions
  • Delimiting statements and blocks with indentation
  • Evaluating Boolean values and expressions

Python provides a complete set of control-flow elements, with loops and conditionals. This chapter examines each in detail.

8.1. The while loop

You’ve come across the basic while loop several times already. The full while loop looks like this:

while condition:    bodyelse:    post-code

condition is an expression that evaluates to a true or false value. As long as it’s True, the body will be executed repeatedly. If it evaluates to False, the while loop will ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required