Recipes: Conditional logic¶
Short, task-focused how-tos for the kinds of conditional work that come up over and over. Each recipe assumes you already know the basics — if a term surprises you, the Learn section is one click away.
Unlike the tutorials, the recipes don't have a recommended order. Pick whichever is useful right now.
Recipes in this guide¶
- Use guard clauses to flatten nested conditions — early returns and raised exceptions as a way to keep the main logic at one indentation level.
- Choose between
if/elifchains, dict dispatch, andmatch/case— three legitimate ways to branch, and the judgement call for which fits when. - Avoid common conditional mistakes —
==vsis, theif x == Trueantipattern, truthy-vs-is None, the chained-comparison surprise, and other footguns.