Journey

Control Flow

This journey follows how a Python program chooses which path runs, names facts at decision points, and exits early when the remaining work no longer applies.

In this journey

  • Choose between paths.
  • Name and shape decisions.
  • Stop as soon as the answer is known.

Choose between paths.

Start with ordinary branching and boolean predicates before reaching for more compact forms.

facts?ifelifelse
Facts flow into one decision point; exactly one branch owns the next step.
  • Booleans

    Use this example to combine facts into readable conditions.

  • Truthiness

    Use this example to use object truth values without hiding intent.

  • Operators

    Use this example to build comparisons and boolean expressions for conditions.

  • Conditionals

    Use this example to choose between branches with clear predicates.

Name and shape decisions.

Some branches become clearer when the code names an intermediate value or dispatches on data shape.

inputname factmatch shape?
Name a fact when a condition needs it; match shape when the data structure is the decision.
  • Assignment Expressions

    Use this example to name an intermediate value inside a condition when it improves clarity.

  • Match Statements

    Use this example to dispatch on the shape of data rather than only on boolean tests.

  • Advanced Match Patterns

    Use this example to combine destructuring, alternatives, and guards in pattern matching.

Stop as soon as the answer is known.

Early exits make the successful path easier to read by moving exceptional or completed cases out of the way.

abcdefirst trueanswer
Early exits draw a boundary: once the answer is found, the tail stays unread.
  • Guard Clauses

    Use this example to show how early returns reduce nested conditional code.

  • Assertions

    Use this example to state assumptions that should fail loudly while developing.

  • Exceptions

    Use this example to leave the current path when ordinary return values are not enough.