How Python Executes Code
The most important execution rule in this topic is simple: when an error occurs in a script and execution is not successfully recovered, Python stops before executing the following statement.
Execution Can Stop Suddenly
When Python executes a script, an error can interrupt the script before the next statement runs. The script stops and displays a traceback instead of continuing to the following statement. This makes execution errors important control-flow events: they do not merely report a problem; they change what the script does next.
The most important execution rule in this topic is simple: when an error occurs in a script and execution is not successfully recovered, Python stops before executing the following statement.
A Function Call in Motion
A function call temporarily moves execution into the function so the function can work with the supplied arguments. In the source example, the addtwo function is called with 3 and 5. Inside the function, the parameters a and b receive 3 and 5 respectively. The function adds those values, stores the sum in its local variable named added, and uses return to send the computed result back to the calling code. The calling code assigns that result to x and then prints x.
Following the addtwo result
Track the value as the addtwo function is called with 3 and 5.
- 1Arguments supplied
The calling code supplies 3 and 5 to addtwo.
- 2Parameters receive values
Inside the function, a receives 3 and b receives 5.
- 3Sum computed
The function computes the sum and places it in the local variable added.
- 4Result returned
The return statement sends the computed result back to the calling code.
- 5Result used
The calling code assigns the returned result to x and prints x.
The printed result is 8 because the function was called with 3 and 5.
This example shows several stages of execution: values enter a function as arguments, the function works with its parameters and local variable, return sends a value outward, and the calling code uses the returned value. Following these stages is a useful way to understand what Python is doing rather than viewing a function call as a single unexplained action.
The Error Path
The error path has two visible results. First, Python produces a traceback. Second, execution stops, so the statement that follows the error is not executed. The traceback is therefore associated with the point where the script failed, while the missing output from the following statement is evidence that control never reached it.
Mistakes About Control Flow
Common mistake Assuming the statement after an error still runs.
What it looks likeA script encounters an error and the learner expects the next print statement to execute.
Why it failsThe script stops when the error occurs, so Python does not execute the following statement.
Do this insteadUse the traceback to identify the failure point, then reason from that point onward: statements after the failure have not been reached.
Common mistake Treating a function call as if it produces its result without intermediate steps.
What it looks likeThe value 8 appears, but the learner does not track the arguments, parameters, local variable, return statement, or assignment to x.
Why it failsThe source example obtains 8 through a sequence: addtwo receives 3 and 5, computes the sum in added, returns it, and the calling code assigns it to x.
Do this insteadTrace the values through the function and then through the calling code.
Common mistake Ignoring the traceback after invalid input causes failure.
What it looks likeThe learner sees an unfriendly error message but does not use it to understand where execution stopped.
Why it failsThe traceback accompanies the script stopping at the error.
Do this insteadRead the traceback as evidence that execution failed before the following statement.
Practice the Trace
Explain the execution of the addtwo example in your own words. Identify the values supplied as arguments, the values received by a and b, the local variable that stores the sum, the action that sends the result back, and the variable that receives the result in the calling code.
Hints
- Start with the values 3 and 5.
- Separate what happens inside the function from what happens in the calling code.
- End by explaining why the printed result is 8.
A script receives invalid input and fails with an error message. Predict what happens to the statement immediately after the failure, then explain how the traceback helps you reason about the point where execution stopped.
Hints
- The script does not continue past the error.
- The following statement is not executed.
- Connect the traceback with the failure point rather than treating it as ordinary program output.
Execution Takeaways
- 1A function call passes argument values to parameters, performs its work, and can return a result to the calling code.
- 2In the addtwo example, 3 and 5 produce 8; the result is returned, assigned to x, and printed.
- 3An error in a Python script produces a traceback and stops execution at the failure.
- 4The statement following an uncaught error is not executed.
- 5A traceback helps connect the failure with the point where script execution stopped.
At a glance
- Python execution can be understood by tracing values and control flow from one step to the next.
- A function receives arguments through its parameters, computes a result, and can return that result to the calling code.
- When an error occurs in a script, Python displays a traceback and stops before the following statement.
- The absence of the following statement's execution is a direct consequence of the script stopping at the error.