Skip to content

Learn: Conditional logic

Three short tutorials that build up your working knowledge of conditional logic in Python — from the basics of if/elif/else through truthiness idioms and on to structural pattern matching. Each one is a Jupyter notebook — read it in the browser, and edit and run any code cell directly on the page.

The tutorials build on each other, so working through them in order is the fastest path. Each takes roughly fifteen to twenty minutes.

The sequence

  1. If statementsif, elif, else, comparison operators, and combining conditions.
  2. Boolean operators and truthiness — short-circuit evaluation, what and/or actually return, the falsy values, and idiomatic truthiness checks.
  3. Pattern matching with match/case — literal, capture, sequence, mapping, class, OR, and guarded patterns.

Before you start

You'll get more from these if you're already comfortable with Python variables, numbers, and comparison operators. If that's still new, the opening sections of the Data structures guide cover the basics.

You don't need Python installed locally — every tutorial runs in your browser. If you'd rather run them on your own machine, each notebook has a download button in the top-right corner.