Control Flow in Python

Lessons in this section

  1. Conditional Statements

    How to run code only when a test is true: if, else, elif chains and why their order matters, nesting, and the one-line conditional expression.

  2. Pattern Matching

    The match statement: cases, the | pattern for grouping values, case _ as the default, the bare-name trap, and when match beats an if chain.

  3. Loops

    Repeating work with for and while, counting with range(), break, continue and pass, nested loops, avoiding infinite loops, and the loop else.

  4. Functions

    Writing and calling functions, every kind of argument, variable scope and the LEGB rule, packing and unpacking, annotations and docstrings.