So you've got the routing function. Forty-seven lines of if/elif, all the order types stacked on top of each other. How does it feel to open that file?
Like dread. It works, but I'm scared to touch it. Adding a new order category means reading the whole thing to figure out where to insert without breaking something.
That dread is your code talking. When a conditional chain gets long enough that you need to read all of it before adding to it, the structure has outgrown the tool. Python has had a better tool for this since 3.10.
match? I've seen that in other languages but I didn't know Python had it.
Structural pattern matching. Not just value matching — it can inspect the shape of your data. A dict with a certain set of keys. A tuple with a certain structure. A string that matches a pattern. Here's a taste of what your routing function becomes:
match order:
case {"type": "express", "weight": w} if w <= 5:
return "priority-belt"
case {"type": "express"}:
return "heavy-express-dock"
case {"type": "standard"}:
return "standard-belt"
case _:
return "manual-review"Each case reads like a description of the data rather than a condition about it. That's actually cleaner than what I had.
And it scales. We'll also cover range() and loop control — break, continue, and the for/else clause you've probably never used. By Friday you'll rewrite that routing function and it will be the kind of code you're not afraid to hand to someone else.
If it's half as readable as that example, Diane might actually understand what it does.
Let's not go that far. But your future self will understand it. That's the goal.
Python's control flow goes deeper than the for loops and if statements you learned in Track 1. Three concepts this week fill in the gaps.
range() is one of Python's most used built-ins, but beginners often use it only in its simplest form: range(10). The full signature is range(start, stop, step). Generating SKU codes, scanning warehouse aisles in reverse, stepping through every third item in a bin — all of these are range() problems. The key insight is that range() doesn't store a list. It's a lazy sequence that computes values on demand, which is why range(1_000_000) uses no more memory than range(10).
break, continue, and for/else give you precise control inside loops. break exits immediately — useful when you find the first matching SKU in a scan and don't need to continue. continue skips the current iteration and moves to the next — useful when processing items but skipping damaged or flagged entries. The else clause on a for loop is Python's most surprising feature for newcomers: it runs only if the loop completed without hitting a break. The canonical use is a search that needs to signal 'not found' — you write else: return None and it only executes if the search exhausted every option.
Match statements are the centerpiece of this week. Python 3.10 introduced structural pattern matching, and it changes how you write decision logic over structured data. Unlike a chain of if/elif blocks that tests one variable at a time, a match statement can inspect the shape of a value: what keys a dict has, what types a tuple contains, whether a string matches a literal. Cases are checked top to bottom and the first match wins. Guard clauses (case pattern if condition) let you add extra conditions without nesting. Wildcard case _: catches everything that didn't match above.
The pass statement and PEP 8 close the week. pass is Python's explicit no-op — it lets you write structurally complete code with empty bodies while you're building. PEP 8 is the style guide that makes Python code readable across teams: naming conventions, spacing around operators, line length limits. Following it isn't about aesthetics. It's about writing code that other Python developers can navigate without translating your personal style.
Sign up to save your notes.
So you've got the routing function. Forty-seven lines of if/elif, all the order types stacked on top of each other. How does it feel to open that file?
Like dread. It works, but I'm scared to touch it. Adding a new order category means reading the whole thing to figure out where to insert without breaking something.
That dread is your code talking. When a conditional chain gets long enough that you need to read all of it before adding to it, the structure has outgrown the tool. Python has had a better tool for this since 3.10.
match? I've seen that in other languages but I didn't know Python had it.
Structural pattern matching. Not just value matching — it can inspect the shape of your data. A dict with a certain set of keys. A tuple with a certain structure. A string that matches a pattern. Here's a taste of what your routing function becomes:
match order:
case {"type": "express", "weight": w} if w <= 5:
return "priority-belt"
case {"type": "express"}:
return "heavy-express-dock"
case {"type": "standard"}:
return "standard-belt"
case _:
return "manual-review"Each case reads like a description of the data rather than a condition about it. That's actually cleaner than what I had.
And it scales. We'll also cover range() and loop control — break, continue, and the for/else clause you've probably never used. By Friday you'll rewrite that routing function and it will be the kind of code you're not afraid to hand to someone else.
If it's half as readable as that example, Diane might actually understand what it does.
Let's not go that far. But your future self will understand it. That's the goal.
Python's control flow goes deeper than the for loops and if statements you learned in Track 1. Three concepts this week fill in the gaps.
range() is one of Python's most used built-ins, but beginners often use it only in its simplest form: range(10). The full signature is range(start, stop, step). Generating SKU codes, scanning warehouse aisles in reverse, stepping through every third item in a bin — all of these are range() problems. The key insight is that range() doesn't store a list. It's a lazy sequence that computes values on demand, which is why range(1_000_000) uses no more memory than range(10).
break, continue, and for/else give you precise control inside loops. break exits immediately — useful when you find the first matching SKU in a scan and don't need to continue. continue skips the current iteration and moves to the next — useful when processing items but skipping damaged or flagged entries. The else clause on a for loop is Python's most surprising feature for newcomers: it runs only if the loop completed without hitting a break. The canonical use is a search that needs to signal 'not found' — you write else: return None and it only executes if the search exhausted every option.
Match statements are the centerpiece of this week. Python 3.10 introduced structural pattern matching, and it changes how you write decision logic over structured data. Unlike a chain of if/elif blocks that tests one variable at a time, a match statement can inspect the shape of a value: what keys a dict has, what types a tuple contains, whether a string matches a literal. Cases are checked top to bottom and the first match wins. Guard clauses (case pattern if condition) let you add extra conditions without nesting. Wildcard case _: catches everything that didn't match above.
The pass statement and PEP 8 close the week. pass is Python's explicit no-op — it lets you write structurally complete code with empty bodies while you're building. PEP 8 is the style guide that makes Python code readable across teams: naming conventions, spacing around operators, line length limits. Following it isn't about aesthetics. It's about writing code that other Python developers can navigate without translating your personal style.