My Python for AI 101 · Chapter 06
Control flow
Seven cards on control flow: ComprehensionOne line that builds a list, a set or a dict from another collection, with an optional filter.See in the glossary →, walking backwards, the bounded loop, the Transition tableThe list of allowed (state, event) pairs and the state each one leads to. Any missing pair is refused, and the state doesn't move.See in the glossary → and ClosureAn inner function that keeps access to the variables of the function that created it.See in the glossary →.
Card 23 — Comprehensions and iteration tools
Project. Dev3Pack AI Engineering Bootcamp
In one sentence. A comprehension builds a list, a setA collection with no duplicates; & gives the shared elements, <= checks that one set is contained in another.See in the glossary → or a Dictionary (dict)A collection of values stored under keys; the shape of the trace, the receipts and the JSON answers.See in the glossary → in one line; next, any, dict.fromkeys and sorted(key=…) cover most of the rest.
Why it matters for an AgentA program that uses a language model to decide what to do (search, call a tool, answer, refuse), in a loop, with stopping rules.See in the glossary →. Filtering invented CitationThe ID of the document that supports an answer. A correct citation doesn't prove the sentence comes from the document.See in the glossary →, removing duplicate documents, finding a product on the menu: a mistake here becomes a false citation or a wrong purchase.
Analogy. Sorting a statement: “every line above €100”, “the first line from this supplier, otherwise nothing”.
The mechanism.
fabricated = [c for c in answer.citations if c not in retrieved_ids]
product = next((p for p in listing["products"] if p["name"] == wanted), None)
unique = list(dict.fromkeys(hits)) # removes duplicates, keeps the order
del state.episodes[:-5] # keeps only the last 5
kept = [d for d, s in sorted(best.items(), key=lambda kv: (-kv[1], kv[0]))
if s >= 0.6 * top][:2]
Input → output. ['rag-basics', 'rag-basics', 'agent-loops'] → ['rag-basics', 'agent-loops']. next(…, None) on “Latte” → None, not an ExceptionAn error signal that interrupts the normal flow; you raise it, catch it (except) and turn it into a refusal.See in the glossary →. Seven episodes → ['Q3', 'Q4', 'Q5', 'Q6', 'Q7']. Scores rag-basics 7.2, agent-loops 5.0, prompt-injection 3.1 → ['rag-basics', 'agent-loops'] (60% threshold, two at most).
Where to read it again. agent.py · fabricated (course) · ch10 · cell 20 (next) · ch08 · cell 30 (dict.fromkeys) · ch11 · cell 8 (del …[:-5]) · ch12 · cell 16 (any(wildcard(s) …)) · grounded-research-agent/agent.py · kept_docs · retrieval.py · zip(…, strict=True).
To reuse. next(gen, None) to search without crashing. dict.fromkeys to remove duplicates without losing the order. zip(a, b, strict=True) when two lists must have the same length (retrieval.py).
Card 24 — A list comprehension with a filter
Project. AI Support Agent (AWS Scholars, chapter 2)
In one sentence. [o for o in orders.values() if o["customer_id"] == cid] builds the filtered list of a customer’s orders in a single expression, instead of a for loop plus append.
Why it matters for an agent. A Lambda tool that answers “my orders” must filter a dataset on every call; the readability of this line matters when the code is read by a reviewer who only has a few minutes.
Analogy. Sorting your mail as you take it out of the letterbox, rather than taking everything out first and sorting it on the table.
The mechanism.
cid = params.get("customer_id", "").upper()
result = [o for o in orders.values() if o["customer_id"] == cid]
Input → output. cid="CUST-123" on the mock dataset → a list of 2 dicts (ORD-001, ORD-002); the orders of CUST-456 don’t appear.
Where to read it again. order_tracker.py L121–127
To reuse. As soon as the logic is “a filter + a transformation”, the comprehension is the idiomatic Python way: shorter to write and quicker to read than a manual loop.
Card 25 — Comprehensions and ** unpacking
Project. NovaMart Multi-Agent Support (AWS Scholars, chapter 3)
In one sentence. A comprehension builds a list or a dict in one expression; **d turns a dict into keyword arguments, or merges its keys into a new dict.
Why it matters for an agent. The same guardrail_settings() function feeds create_guardrail and update_guardrail (the tuning script): a single source of truth for the safety policies.
Analogy. A pre-filled form handed in as it is at two different counters.
The mechanism.
update_expr_parts = [f"{k} = :{k}" for k in updates]
expr_values = {f":{k}": v for k, v in updates.items()}
response = bedrock_client.create_guardrail(**guardrail_settings())
return {'success': False, **denied}
Input → output. Replayed: {'inventory_agent': 'facts'} → SET inventory_agent = :inventory_agent, version = :new_version and {':inventory_agent': 'facts'}, the exact format of an UpdateExpression. {'success': False, **{'access_denied': True, 'message': 'x'}} → {'success': False, 'access_denied': True, 'message': 'x'}.
Where to read it again. agent_orchestrator.py · _update_workflow_state · agent_orchestrator.py · topics_config · agent_orchestrator.py · create_guardrail(**…) · tune_guardrail.py · _update_draft.
To reuse. A function that returns a configuration dict, unpacked with ** wherever it is used. A comprehension as long as it fits on one readable line; a loop otherwise.
Card 26 — reversed() + early exit
Project. AI Support Agent (AWS Scholars, chapter 2)
In one sentence. The memory save walks through agent.messagesThe conversation history a Strands agent reads again on every call.See in the glossary → backwards with reversed() and leaves with break as soon as the last user message AND the last assistant message are found.
Why it matters for an agent. A conversation’s history only grows; looking for “the last pair” from the start would mean scanning the whole history again on every turn, at a cost that grows with the conversation.
Analogy. Looking for your keys in the last pocket you put them in, not by emptying every bag one by one from the first.
The mechanism.
for message in reversed(messages):
content = message.get("content", [])
if not content or "text" not in content[0]:
continue
if message.get("role") == "assistant" and agent_response is None:
agent_response = content[0]["text"]
elif message.get("role") == "user" and customer_query is None:
customer_query = content[0]["text"]
if customer_query and agent_response:
break
Input → output. A 40-message history → the loop stops once the last 2 or 3 relevant messages are found, not after reading all 40.
Where to read it again. main.py L161–170
To reuse. To find “the most recent item that meets a condition” in a list, walk it with reversed() and leave as soon as it is found, rather than scanning the whole list in the normal order.
Card 27 — The bounded loop: exits first
Project. Dev3Pack AI Engineering Bootcamp
In one sentence. An Agent loopThe cycle message → model → tool call → result → model, repeated until an answer or a planned exit.See in the glossary → checks its stop conditions before every action, and always returns the same receipt, whatever the exit.
Why it matters for an agent. With no exit, the loop keeps running, spends, and ends in a traceback. Four exits are enough: answer, repetition, BudgetThe maximum number of calls an agent loop may make, checked before each call, never after.See in the glossary →, tool error.
Analogy. A bank card’s limit: checked before the payment, not after.
The mechanism.
for step in plan:
name, args = step["tool"], step.get("args", {})
if name == "answer":
return receipt(steps, "answered", answer=args["text"])
if (name, args) == previous: # the same call twice
return receipt(steps, "repeated_call", refusal=...)
if len(steps) >= budget: # the budget, BEFORE the call
return receipt(steps, "budget", refusal=...)
try:
result = tools[name](**args) # the dict becomes keyword arguments
except ToolError as error: # the error becomes a refusal
return receipt(steps, "tool_error", refusal=...)
steps.append({"tool": name, "args": args, "result": result})
previous = (name, args)
Input → output. echo("a") twice → stopped_because = 'repeated_call'. echo, then answer → answer = 'done'.
Where to read it again. ch05 · cells 13 and 24 (run_loop, then respond, the challenge bot) · agent.py · a single retry when the JSON is broken (course) · grounded-research-agent/agent.py · for attempt in (1, 2) · ch08 · cell 28 (MAX_VISITS).
To reuse. Write the exit returns before the body. A single receipt format: steps, stopped_because, answer, refusal. Every exit other than the answer writes a sentence for a reader.
Card 28 — The transition table
Project. Dev3Pack AI Engineering Bootcamp
In one sentence. A dict whose keys are (state, event) pairs describes the whole graph; a missing pair is refused.
Why it matters for an agent. The agent cannot answer before it has searched. The ban is a missing key, visible at a glance.
Analogy. A payment’s path: entered, approved, sent. “Sent” straight from “entered” does not exist.
The mechanism.
TRANSITIONS = {
("planning", "plan_ready"): "retrieving",
("retrieving", "hits"): "answering",
("answering", "answered"): "done",
}
target = TRANSITIONS.get((current, name)) # None if the pair does not exist
new = {"state": target, "visited": [*state["visited"], target], "rejected": None}
Input → output. plan_ready from planning → retrieving, visited = ['planning', 'retrieving']. answered from planning → rejected = "'answered' is not a declared event in state 'planning'".
Where to read it again. ch08 · cells 20, 22, 26 and 28.
To reuse. A tuple as a dict key. .get, which returns None instead of raising. [*old, new] for a fresh list. A visit counter to bound the retries.
Card 29 — Closures: tools packed with their rules
Project. NovaMart Multi-Agent Support (AWS Scholars, chapter 3)
In one sentence. A function defined inside another keeps access to the enclosing function’s variables, even after that function has finished.
Why it matters for an agent. Each build_*_agent() defines its tools inside, with their constants (return_windows). No global variable, and every agent stands on its own (rubric criterion: “self-contained” builders).
Analogy. An adviser who goes to a meeting with the branch’s price list in their bag.
The mechanism.
def build_refund_agent() -> Agent:
return_windows = {'Standard': 30, 'Premium': 60}
@tool(context=True)
def initiate_refund(customer_id, order_id, reason, tool_context):
window = return_windows.get(tier, return_windows['Standard']) # captured
...
# Orchestrator: one dict shared by the hooks and the routing tools
turn = {'session_id': session_id, 'customer_id': None, 'request': '', ...}
turn.update(session_id=sid, customer_id=cid, request=request) # mutate, never rebind
Input → output. A Premium customer whose order is 45 days old → window = 60 → return possible; Standard → window = 30 → RefusalAn explicit "I can't answer": no citation, zero confidence, human review. A state, not a crash.See in the glossary →. The turn dict is changed in place (turn.update): if it were reassigned (turn = {...}) in a HookCode triggered at a precise moment in the agent's life, before or after a call, to enforce a rule the prompt doesn't guarantee.See in the glossary →, the tools would still see the old one.
Where to read it again. agent_orchestrator.py · return_windows · agent_orchestrator.py · turn.
To reuse. An agent’s tools defined inside its builder. State shared between inner functions = a dict or an object you change, never a variable you reassign.
In the field
The chapters of my courses and my projects where these mechanisms run for real.
- Dev3Pack, Session 5: card 27.
- Dev3Pack, Session 8: cards 23, 27 and 28.
- Dev3Pack, Session 10: card 23.
- Dev3Pack, Session 11: card 23.
- Dev3Pack, Session 12: card 23.
- Dev3Pack, Final assignment: cards 23 and 27.
- AWS Scholars, chapter 2 · AI Support Agent: cards 24 and 26.
- AWS Scholars, chapter 3 · NovaMart Multi-Agent Support: cards 25 and 29.