liveBook · Manning

Chapter 8: Control flow

What you will learn in chapter 8 of The Quick Python Book, Fourth Edition

Introduces the chapter’s coverage of Python control flow, including conditionals, loops, comprehensions, indentation, and Boolean expressions.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces the chapter’s coverage of Python control flow, including conditionals, loops, comprehensions, indentation, and Boolean expressions.

8.1 The if-elif-else statement

This section explains how Python’s if-elif-else statement selects a branch and what happens when clauses or bodies are omitted.

Key terms: if, elif, else, condition, body, pass statement

8.2 Structural pattern matching with match

This section introduces match-case as structural pattern matching, explains how cases select code, and cautions that complex patterns need careful testing.

Key terms: match-case statement, structural pattern matching, test_expression, or operator |, case for "_", pattern matching, edge cases

8.3 The while loop

This section explains how a while loop repeats, how its optional else block behaves, and how break and continue change the loop’s flow.

Key terms: while loop, condition, body, post-code, indentation, else, break, flag, continue

8.4 The for loop

This section explains how Python for loops visit values from an iterable and how break, continue, and an optional else clause affect execution.

Key terms: for loop, iterable object, item, body, break, continue, else, post-code

8.4.1 The range function

This subsection shows how to generate list indices for a for loop with range(len(...)) and explains why range uses little memory.

Key terms: range, len, indices, for loop, range object, integers on demand

8.5 Controlling range with starting and stepping values

This section explains how starting, ending, and step arguments control the sequence produced by range.

Key terms: range, starting value, ending value, step value

8.6 The for loop and tuple unpacking

Shows how tuple unpacking makes a for loop over pairs cleaner by assigning each pair’s elements to separate variables.

Key terms: tuple unpacking, for loop, two-element tuples

8.7 The enumerate function

This section shows how enumerate pairs each list item with its index for clearer loops.

Key terms: enumerate function, tuple unpacking, index, item, range

8.8 The zip function

This section explains how zip pairs corresponding elements of iterables for use in a loop.

Key terms: zip function, iterables, tuples

8.9 List, set, and dictionary comprehensions

The section introduces comprehensions as concise ways to build lists, sets, and dictionaries while iterating over and optionally filtering a sequence.

Key terms: comprehension, for loop, conditional expression, list comprehension, set comprehension, dictionary comprehension, key and value expression

8.9.1 Generator expressions

Introduces generator expressions as a memory-efficient alternative to list comprehensions for producing values to iterate over.

Key terms: generator expression, generator object, iterator, list comprehension

8.10 Statements, blocks, and indentation

This section explains how indentation defines Python blocks, how indentation mistakes arise, and how to continue statements across lines.

Key terms: block, body, compound statement, clause, nested blocks, indentation, tabs, spaces, four spaces, backslash

8.11 Boolean values and expressions

Introduces Python’s Boolean values and the result of Boolean operations in the context of conditional tests.

Key terms: Boolean object type, True, False, Boolean operation

8.11.1 Most Python objects can be used as Booleans

This subsection explains how Python treats common objects as true or false in a Boolean context.

Key terms: Boolean context, False, True, None

8.11.2 Comparison and Boolean operators

This section explains comparison and Boolean operators, including membership and identity tests, compound conditions, and how and/or return operands.

Key terms: ==, !=, in, not in, is, is not, and, or, not, precedence

8.12 Writing a simple program to analyze a text file

The section uses a small word-counting program to illustrate how concise a Python program can be and how to run it against a sample file.

Key terms: UNIX wc utility, lines, words, characters, for loop, idiomatic, conciseness, word_count.py, word_count.tst, notebook

8.13 Refactoring word_count

This section poses an exercise to shorten a word-count program, with an optional change to count only alphabetic strings.

8.13.1 Solving the problem with AI-generated code

This subsection explains what to give an AI chatbot when asking it to refactor existing word-counting code.

Key terms: refactoring, AI chatbot, alphabetic strings

8.13.2 Solutions and discussion

8.14 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 7: Dictionaries Chapter 9: Functions →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.