Menu

Explain Code with AI: Line by Line Prompts

Ask AI to explain code line by line, at your level, with a table of how the values change as it runs. Say what you already know so the explanation starts where you are.

Every prompt below is editable: change it, then open it in ChatGPT, Claude or another AI app.

To get a useful explanation of code from AI, say how much you already know, ask for the explanation line by line, and ask for a trace: a table of the values each variable holds as the code runs. A bare "explain this code" gets a summary written for an imaginary average reader, which is usually either too basic or too fast for you.

Line by line, with a trace of values

The two tabs ask about the same short function. Compare what each reply lets you check.

Explain this code: def running_max(nums): result = [] best = None for n in nums: if best is None or n > best: best = n result.append(best) return result print(running_max([3, 1, 4, 1, 5]))
Try it
Example replyReplies vary between models and runs.

This function computes the running maximum of a list. It loops over the numbers, keeps track of the largest value seen so far in best, and appends that value to result at every step. The best is None check handles the first element. The call prints [3, 3, 4, 4, 5].

The function runs in O(n) time, since it visits each element once.

The first reply is accurate, and it only helps if you already know what a running maximum is. It also adds a note about time complexity that nobody asked for, the kind of extra you get when the model does not know who is reading. The second reply ties every sentence to a line you can point at, and the table makes concrete what the summary only states in passing: result.append(best) runs on every pass, including the passes where best does not change. That is why 3 appears twice, and you can see it in row 2 instead of inferring it from "at every step".

A trace table is also a check on the explanation itself. You can verify each row by running the code with a print(n, best, result) inside the loop. If a row disagrees with the real output, the explanation is wrong somewhere, and now you know where.

Explain it at your level

The single most useful line in an explanation prompt is what you already know. It tells the model which lines to skip and which to slow down on. A role sentence can help too ("You are a tutor for people in their first month of coding"), but it works through the same mechanism: it tells the model who the reader is. Role prompting covers what a role changes and what it does not.

This block is a reusable version. Change the level, the confusing part or the code, and the explanation changes with it.

Explain at my level
Fill in
Parts
Explain the code below to me.
My level: I know loops, lists and dictionaries, but not lambda or sorted with a key. The part that confuses me: the last two lines.
text = "the cat and the hat" counts = {} for word in text.split(): counts[word] = counts.get(word, 0) + 1 top = sorted(counts.items(), key=lambda kv: kv[1], reverse=True)[:2] print(top)
Explain the lines I already understand in one sentence in total. Spend the rest on the confusing part, piece by piece, showing the value each piece produces for this input.
Under 180 words. Use only the concepts in my level, plus the ones you are explaining.
Try it
Example replyReplies vary between models and runs.

The loop counts each word: counts ends as {'the': 2, 'cat': 1, 'and': 1, 'hat': 1}.

Now the last two lines, from the inside out:

  • counts.items() gives the pairs ('the', 2), ('cat', 1), ('and', 1), ('hat', 1).
  • lambda kv: kv[1] is a tiny function with no name. It takes one pair and returns its second item, the count. For ('the', 2) it returns 2.
  • sorted(..., key=...) sorts the pairs by what that function returns, so it sorts by count. reverse=True puts the biggest first. Words with equal counts keep their original order.
  • [:2] keeps the first two pairs.

So top is [('the', 2), ('cat', 1)], and that is what is printed.

"Equal counts keep their original order" is the detail a quick explanation leaves out, and it is why cat wins the second place and not and or hat. Python's sorted is stable, and reverse=True keeps that stability. You would not think to ask about it, and asking for "the value each piece produces for this input" is what brought it to the surface.

Ask about one part, not the whole file

Pasting a 500-line file and asking "what does this do" gets a tour of the file's structure. When you are stuck on one function, paste that function and the lines that call it, and ask about it directly: "Why does this function return early when the list is empty?" or "What does yield do on line 12, and what would change if it were return?" A narrow question gets an answer about your code; a broad one gets an answer about code like yours.

For a large unfamiliar codebase, turn it around: ask for a map first ("list the main modules and what each one is responsible for, in one line each"), then ask about the module you need. This is a small prompt chain, and each step keeps the model's answer short enough to check.

Explain it through a language you know

Code in an unfamiliar language is easier to read when the explanation is anchored in one you know. Say so in the prompt: "I know Python. Explain this Go function and point out every place where it behaves differently from what a Python programmer would expect." The differences are where the misunderstandings live: integer division, zero values instead of None, a slice that shares memory with the array it came from. A plain explanation of the Go code would describe those lines correctly and never tell you that your instincts about them are wrong.

Check that you understood

An explanation you read and agree with can still leave you unable to write the code yourself. Two prompts close that gap:

  • Predict first. Before you run the code or ask for an explanation, write down what you think it prints. Then ask the model to explain only the lines where your prediction was wrong.
  • Explain it back. Write your own explanation and ask the model to point out anything wrong or missing, without rewriting it.
Quiz me on this code
Fill in
I just read an explanation of this Python code: def running_max(nums): result = [] best = None for n in nums: if best is None or n > best: best = n result.append(best) return result Ask me 3 questions that check whether I really understand it, one at a time. Include one question where I must predict the output for a new input. Wait for my answer before asking the next question, and tell me if my answer is right and why.
Try it
Example replyReplies vary between models and runs.

Question 1 of 3: what does running_max([2, 7, 5, 9]) return? Work it out before you run it.

The same approach works when a program is misbehaving, but then you want the cause, not a lesson: see prompts for debugging. When the code in front of you was written by an AI, a line-by-line explanation is a quick way to review it before you keep it; prompts for writing code lists the other checks to run.

Frequently Asked Questions

How do I ask ChatGPT or Claude to explain code?

Paste the code and say three things: what you already know, which part confuses you, and what shape you want the explanation in. "Explain each line in one sentence, then show a table of the variables after each loop pass" gets a far more useful answer than "explain this code".

What is a trace table?

A trace table follows a program as it runs: one row per step (often one loop pass), one column per variable, and the value of each variable at that point. Asking for one turns an abstract description into concrete values you can check against the code. It is a written record of what a debugger shows you as you step through a program.

Can AI explain code in a language I do not know?

Yes, and it is one of the most useful things to ask for. Name a language you do know and ask the model to compare: "I know Python; explain this Rust code and point out where it behaves differently from Python." Check the claims about language behavior by running small examples, since explanations can be confidently wrong on details.

Is using AI to explain code bad for learning?

It depends on what you do next. Reading an explanation and moving on teaches little. Predicting the output before you run the code, asking the model to quiz you, or explaining the code back and asking what you got wrong all make you do the thinking, and that is where the learning happens.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED