Not line by line
The topic Debugging and profiling taught you to measure. timeit times a statement, cProfile shows which function eats the time, and pdb walks the frames of a running program. Two questions stayed open. Why does one operation cost nanoseconds and another many times more? And what exactly are the frames that the debugger walks? This topic opens the Under the hood module and answers both from inside the interpreter.
A common picture of an interpreter is a program that reads a line of text, works out what it means, does it, and reads the next line. If CPython worked like that, a loop that runs a million times would parse its body a million times. A syntax error on the last line would show up only after the first lines had run. And nothing could make the same line faster on its second run.
None of that holds. The topic Running Python gave the outline: CPython first compiles the whole file into bytecode, and then a loop written in C executes the bytecode. So a syntax error anywhere stops the file before its first line runs, and a loop body is analysed once. The same line even gets faster after a few runs, as a later block shows.
This topic follows one small function through every stage:
def f(price, qty):
total = price * qty
return total + 1The stages are tokens, a syntax tree, bytecode, the evaluation loop with its value stack and frames, and the rewriting of instructions as the interpreter learns the types it sees. Then comes memory: reference counts, the cycle collector and immortal objects.
All output here comes from CPython 3.14.7. Instruction names, specializations and collector thresholds are CPython details. Bytecode differs between versions: even 3.13 prints a different listing for f. Other implementations, such as PyPy, have none of this.