So far, every program we’ve seen runs top to bottom, one line at a time. Control flow is how we change that — making decisions with if, repeating actions with loops, and picking between options with pattern matching.

What’s in this section

  1. if, elif, else — making decisions
  2. for loops — repeat an action for each item in a collection
  3. while loops — repeat as long as a condition is true
  4. break, continue, pass — control the flow inside loops
  5. range() and iterables — what loops actually loop over
  6. Pattern matching with match/case — modern Python’s switch statement

After this section, you’ll be able to write programs that respond to different inputs, repeat work, and process collections of data. That’s most of what real programs do.

Toggle theme (T)