Reading Python with AI as Your Explainer
Why This Matters
The old way of learning Python was a tour of the language: data types, operators, loops, functions, classes. You still need those ideas, but you learn them much faster by reading real code that does a real job, with an AI assistant answering your questions. This lesson shows you how.
Read a real function
Open intake/validate.py:
ORDER_NUMBER = re.compile(r"^PO-\d{5}$")
MAX_PRICE_DEVIATION = 0.05 # 5 % above or below the catalog price
def validate(order: Order, catalog: dict, today: date) -> list[str]:
"""Return a list of issues. An empty list means the order can be booked automatically."""
issues = []
if not ORDER_NUMBER.match(order.order_number):
issues.append(f"Order number {order.order_number!r} does not match PO-12345")
if order.delivery_date < today:
issues.append(f"Delivery date {order.delivery_date} is in the past")
if not order.lines:
issues.append("Order has no lines")
for line in order.lines:
item = catalog.get(line.sku)
if item is None:
issues.append(f"Unknown SKU {line.sku}")
continue
if line.quantity <= 0:
issues.append(f"{line.sku}: quantity must be at least 1, got {line.quantity}")
deviation = abs(line.unit_price - item["price"]) / item["price"]
if deviation > MAX_PRICE_DEVIATION:
issues.append(f"{line.sku}: price {line.unit_price:.2f} differs {deviation:.0%} from catalog {item['price']:.2f}")
return issues
Without knowing Python you can already see a lot: a list of issues is built up by a series of if checks, one rule each, and returned at the end.
The Python you meet here
| You see | It means | The old course called it |
|---|---|---|
def validate(order: Order, ...) -> list[str]: | A function, with the types of its inputs and output | Functions and modules |
issues = [] and issues.append(...) | A list you add items to | Data types and variables |
if ... : / for line in order.lines: | A decision / a loop over each item | Conditions and loops |
catalog.get(line.sku) | Look up a key in a dictionary; None if it is missing | Data types |
f"... {line.sku} ..." | A text with values filled in (an f-string) | Strings |
continue | Skip to the next item in the loop | Loops |
Order, OrderLine in models.py | Data classes: named bundles of fields | Object-oriented programming |
Indentation matters in Python: everything indented under an if or for belongs to it.
Let AI explain, then check
Explain intake/validate.py line by line for a tester who is new to Python.
Then list every rule it checks, with one example order that breaks that rule
and the exact issue text it produces.
Then check the answer against reality. The real expected texts are in tests/test_validate.py. If the AI’s example text differs from the test, find out which one is right. That habit of checking an explanation against evidence is the core skill of this course.
Exercise
- Ask your AI assistant to explain
validate.pywith the prompt above. - Compare its list of rules with the code. Did it miss one, or invent one?
- Open
intake/models.pyand ask the AI what@dataclass(frozen=True)means and whytotalis a property. Check one claim by trying it in a Python shell (pythonin the terminal). - Write down one question about the rules that the code cannot answer. (For example: is a price exactly 5% above the catalog accepted?) Keep it for lesson 8, Testing Business Rules: Boundaries and Breaking It on Purpose.
Key Takeaways
- Learn Python by reading real code that does a real job.
- Functions, lists, dictionaries, decisions, loops and data classes cover most of what you will read.
- Let AI explain, then check the explanation against the tests.
- Questions the code cannot answer are findings worth taking to the business.