Syllabus Lesson 25 of 141 · Use AI to understand
Use AI to understand

Explain it back

Why it matters Code that looks right is the easiest kind to accept without understanding. Predicting its output on awkward inputs is the fastest way to find out whether you really read it.

An assistant wrote these two helpers. They look fine. Read them carefully.

# Written by an AI assistant for a listings page.
def page_slice(items, page, per_page=10):
    start = (page - 1) * per_page
    return items[start:start + per_page]

def merge_settings(defaults, overrides):
    out = defaults
    out.update({k: v for k, v in overrides.items() if v})
    return out

Now predict, without running anything, what each line below gives:

items = list(range(25))
q1: page_slice(items, 3)
q2: page_slice(items, 0)
q3: page_slice(items, -1)
q4: merge_settings({"retries": 3, "debug": True}, {"retries": 0, "debug": False})
q5: d = {"theme": "dark"}; merge_settings(d, {"lang": "en"}); then what is d?

The check compares your predictions with the real results. If one is wrong, that is the useful part: run it, find out why, and only then fix your prediction.

Recap Commit to a prediction, then check. Every miss shows a gap between the code and your picture of it.

Your turn

Assign predictions, a dict with keys "q1" to "q5" holding the values you expect (lists and dicts written out in full). Write your predictions before you run the helpers. Then assign surprise: one sentence, at least six words, on which result surprised you and why. Then, if you like, call the helpers to check.

Spotted a problem in this lesson? Report it · Ask about this lesson

This exercise opens soon

Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.