Yesterday: every item went the same path. Today: items branch. If value > 10, do X. Else, do Y. Same pattern as Foundations' filter, but instead of skip-or-act it's act-A-or-act-B.
items = [{"id": "a", "value": 5}, {"id": "b", "value": 15}]
for item in items:
if item["value"] > 10:
# path A
toolset.execute_action(Action.GOOGLECALENDAR_CREATE_EVENT, {
"calendar_id": "primary",
"summary": f"high: {item['id']}",
"start_datetime": "2026-12-31T10:00:00+00:00",
"event_duration_minutes": 15,
})
else:
# path B — discover task list and add a low-priority task
lists = toolset.execute_action(Action.GOOGLETASKS_LIST_TASK_LISTS, {})
tasklist_id = (lists.get("items") or lists.get("response_data", {}).get("items", []))[0]["id"]
toolset.execute_action(Action.GOOGLETASKS_CREATE_TASK, {
"tasklist_id": tasklist_id,
"title": f"low: {item['id']}",
})Two destinations, one branch per item, every item gets somewhere.
And if/else is from Python Foundations?
Same if/else. The new piece isn't the syntax — it's the shape: tool A on one branch, tool B on the other. The condition is plain Python (item["value"] > 10), but the side effect is different per branch.
What about three branches?
if/elif/elif/else. For 4+ branches, you'd switch to a dispatch dict — routes = {"high": tool_a, "low": tool_b, "medium": tool_c} and routes[category](item). That pattern lives in week 4. Today: two-way branch.
for item in items:
if predicate(item):
path_a(item)
else:
path_b(item)Every item exits to exactly one of path_a or path_b. No item is skipped, no item is double-processed.
Three similar-looking patterns; pick the right one:
| Pattern | Shape | Example |
|---|---|---|
| Filter | act on some, skip others | if x > 10: send(x) (no else) |
| Routing | every item → A or B | if x > 10: send(x) else log(x) |
| Partition | split into two lists, then act on each | bigs = [x for x in xs if x > 10]; smalls = [...] |
Day 5 covers partition; today and yesterday cover the simpler shapes.
In a routing loop, two counters tell you what happened:
a_count = 0
b_count = 0
for item in items:
if item["value"] > 10:
path_a(item)
a_count += 1
else:
path_b(item)
b_count += 1
print(f"path_a: {a_count}, path_b: {b_count}")One extra line per branch; the closing report tells you the routing was sane (5 of 10 high, 5 of 10 low) without re-reading the items.
The if predicate can be as simple as item["value"] > 10 or as complex as a function:
def is_urgent(item):
return item.get("priority") == "high" or item.get("due_in_hours", 999) < 24
for item in items:
if is_urgent(item):
...Extract the predicate into a function once it has 2+ conditions. The branch reads as English ("if urgent, send alert") instead of three nested and/or.
for item in items:
if item["value"] > 100:
critical_path(item)
elif item["value"] > 10:
normal_path(item)
else:
low_path(item)Readable up to ~3 branches. Beyond that, switch to a dispatch dict — covered in week 4.
Yesterday: every item went the same path. Today: items branch. If value > 10, do X. Else, do Y. Same pattern as Foundations' filter, but instead of skip-or-act it's act-A-or-act-B.
items = [{"id": "a", "value": 5}, {"id": "b", "value": 15}]
for item in items:
if item["value"] > 10:
# path A
toolset.execute_action(Action.GOOGLECALENDAR_CREATE_EVENT, {
"calendar_id": "primary",
"summary": f"high: {item['id']}",
"start_datetime": "2026-12-31T10:00:00+00:00",
"event_duration_minutes": 15,
})
else:
# path B — discover task list and add a low-priority task
lists = toolset.execute_action(Action.GOOGLETASKS_LIST_TASK_LISTS, {})
tasklist_id = (lists.get("items") or lists.get("response_data", {}).get("items", []))[0]["id"]
toolset.execute_action(Action.GOOGLETASKS_CREATE_TASK, {
"tasklist_id": tasklist_id,
"title": f"low: {item['id']}",
})Two destinations, one branch per item, every item gets somewhere.
And if/else is from Python Foundations?
Same if/else. The new piece isn't the syntax — it's the shape: tool A on one branch, tool B on the other. The condition is plain Python (item["value"] > 10), but the side effect is different per branch.
What about three branches?
if/elif/elif/else. For 4+ branches, you'd switch to a dispatch dict — routes = {"high": tool_a, "low": tool_b, "medium": tool_c} and routes[category](item). That pattern lives in week 4. Today: two-way branch.
for item in items:
if predicate(item):
path_a(item)
else:
path_b(item)Every item exits to exactly one of path_a or path_b. No item is skipped, no item is double-processed.
Three similar-looking patterns; pick the right one:
| Pattern | Shape | Example |
|---|---|---|
| Filter | act on some, skip others | if x > 10: send(x) (no else) |
| Routing | every item → A or B | if x > 10: send(x) else log(x) |
| Partition | split into two lists, then act on each | bigs = [x for x in xs if x > 10]; smalls = [...] |
Day 5 covers partition; today and yesterday cover the simpler shapes.
In a routing loop, two counters tell you what happened:
a_count = 0
b_count = 0
for item in items:
if item["value"] > 10:
path_a(item)
a_count += 1
else:
path_b(item)
b_count += 1
print(f"path_a: {a_count}, path_b: {b_count}")One extra line per branch; the closing report tells you the routing was sane (5 of 10 high, 5 of 10 low) without re-reading the items.
The if predicate can be as simple as item["value"] > 10 or as complex as a function:
def is_urgent(item):
return item.get("priority") == "high" or item.get("due_in_hours", 999) < 24
for item in items:
if is_urgent(item):
...Extract the predicate into a function once it has 2+ conditions. The branch reads as English ("if urgent, send alert") instead of three nested and/or.
for item in items:
if item["value"] > 100:
critical_path(item)
elif item["value"] > 10:
normal_path(item)
else:
low_path(item)Readable up to ~3 branches. Beyond that, switch to a dispatch dict — covered in week 4.
Create a free account to get started. Paid plans unlock all tracks.