Concepts / How Python Executes Code

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.

  • Programming

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.

Key point

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.

Worked example

Following the addtwo result

Track the value as the addtwo function is called with 3 and 5.

  1. 1
    Arguments supplied

    The calling code supplies 3 and 5 to addtwo.

  2. 2
    Parameters receive values

    Inside the function, a receives 3 and b receives 5.

  3. 3
    Sum computed

    The function computes the sum and places it in the local variable added.

  4. 4
    Result returned

    The return statement sends the computed result back to the calling code.

  5. 5
    Result used

    The calling code assigns the returned result to x and prints x.

Result

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

Python scriptErrorTracebackFollowing statementnot executedexecution reaches a problemerror is reportedexecution stops before it
What happens to control flow after an error occurs, and why is the statement after the error never executed?

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 like

    A script encounters an error and the learner expects the next print statement to execute.

    Why it fails

    The script stops when the error occurs, so Python does not execute the following statement.

    Do this instead

    Use 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 like

    The value 8 appears, but the learner does not track the arguments, parameters, local variable, return statement, or assignment to x.

    Why it fails

    The 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 instead

    Trace the values through the function and then through the calling code.

  • Common mistake

    Ignoring the traceback after invalid input causes failure.

    What it looks like

    The learner sees an unfriendly error message but does not use it to understand where execution stopped.

    Why it fails

    The traceback accompanies the script stopping at the error.

    Do this instead

    Read the traceback as evidence that execution failed before the following statement.

Practice the Trace

EASY

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.
MEDIUM

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

  1. 1
    A function call passes argument values to parameters, performs its work, and can return a result to the calling code.
  2. 2
    In the addtwo example, 3 and 5 produce 8; the result is returned, assigned to x, and printed.
  3. 3
    An error in a Python script produces a traceback and stops execution at the failure.
  4. 4
    The statement following an uncaught error is not executed.
  5. 5
    A 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.

Related concepts

More concepts starting with H