Concepts / Interactive Mode: Running Python Code Line by Line

Interactive Mode: Running Python Code Line by Line

The central difference is where the interpreter gets the code: interactive mode receives code line by line, while script execution receives the name of a file containing the code.

  • Programming

Two Ways to Run Python

Python code can be executed interactively, one line at a time, or from a script file. In interactive use, you type Python code for the interpreter to handle line by line. In file-based execution, you tell the Python interpreter to read and execute the code stored in a file.

Interactive modeCode entered line by linePython scriptCode stored in a filePython interpreterHandles each entered linePython interpreterReads and executes the fileenter codeprovide filename
What changes when Python receives code one line at a time instead of receiving a filename?
Key point

The central difference is where the interpreter gets the code: interactive mode receives code line by line, while script execution receives the name of a file containing the code.

Starting a Script

To run a Python script, type the command python followed by the filename at the operating system command prompt. The filename tells the Python interpreter which file to read and execute.

python

The command is an instruction to the interpreter to work with the named file. This is different from entering a Python statement directly for immediate interactive handling: the interpreter is being given a file as its source of code.

python <filename>Command promptOpen fileInterpreter begins with thenamed fileRead all linesFile contentsExecute each lineTop to bottomProduce outputIf the script producesoutputExitEnd of filefilename suppliedfile openedlines availableexecution may producelast line completed
What happens after you provide a Python filename at the command prompt?

A Top-to-Bottom Trace

Worked example

Following a File-Based Run

Suppose you enter python followed by the name of a Python script at the operating system command prompt. Trace the interpreter's work from the command to termination.

  1. 1
    Provide the filename

    The command tells the Python interpreter which file to use.

  2. 2
    Open the file

    The interpreter begins handling the named script file.

  3. 3
    Read the lines

    The interpreter reads the lines contained in the file.

  4. 4
    Execute in order

    Execution flows from the top of the file toward the bottom, handling each line in order.

  5. 5
    Reach the end

    After the final line has been handled, the interpreter reaches the end of the file.

  6. 6
    Exit automatically

    The interpreter exits when it reaches the end of the file, so the script does not need a quit() command.

Result

A file-based Python run proceeds from the command prompt through the file from top to bottom and then ends automatically.

This trace shows why the filename is important. It identifies the file that supplies the sequence of Python lines. Once the interpreter has executed the lines in order and reached the end of that file, the file-based run is complete.

Why quit() Is Unnecessary

A script has a natural stopping point: the end of its file. Python's interpreter automatically exits when it reaches that point. Therefore, a script file does not need a quit() command merely to tell the interpreter that execution is finished.

Script linesExecuted from top to bottomEnd of fileFinal line completedInterpreter exitsNo quit() requiredexecution reaches final lineautomatic termination
What control flow occurs when the interpreter reaches the final line of a Python script?

Mistakes to Avoid

  • Common mistake

    Thinking that script execution requires entering every line interactively.

    What it looks like

    Treating a script run as though each line must be typed separately at the command prompt.

    Why it fails

    File-based execution tells the interpreter to read the code from a file rather than receiving it line by line.

    Do this instead

    Provide the filename with python <filename> so the interpreter can read and execute the file.

  • Common mistake

    Expecting a script to require quit() at the end.

    What it looks like

    Adding quit() simply to stop execution after the final line.

    Why it fails

    The interpreter automatically exits when it reaches the end of the script file.

    Do this instead

    Allow the file-based run to end naturally when the interpreter reaches the end of the file.

  • Common mistake

    Ignoring execution order within the file.

    What it looks like

    Assuming the interpreter can execute the file's lines in an arbitrary order.

    Why it fails

    Script execution flows from top to bottom, with each line executed in order.

    Do this instead

    Trace a script from its first line through its final line.

Check Your Understanding

MEDIUM

Explain what the interpreter does after a learner enters python <filename> at the operating system command prompt. Include the order of file handling, line execution, and termination.

Hints
  • Begin with the purpose of the filename.
  • Describe the direction in which the lines are executed.
  • State what happens when the interpreter reaches the end of the file.

What do you think happens?

After the interpreter executes the final line of a Python script, what additional command is required to make the interpreter exit?

  • quit()

  • The filename again

  • No additional command

Reveal answer
Answer:

No additional command

The interpreter automatically exits when it reaches the end of the file, so quit() is not needed in a script file.

Key Takeaways

  1. 1
    Running a script means telling the Python interpreter to read and execute code from a file.
  2. 2
    The command for starting a script is python <filename>, entered at the operating system command prompt.
  3. 3
    Script execution proceeds from the top of the file to the bottom, one line after another.
  4. 4
    The interpreter automatically exits at the end of the file, so quit() is unnecessary in a script.

At a glance

  • Interactive mode handles Python code one line at a time, while script execution reads code from a file.
  • Use python <filename> at the operating system command prompt to run a script.
  • The interpreter opens the file, reads its lines, executes them from top to bottom, and produces output as the script runs.
  • When the interpreter reaches the end of the file, it exits automatically; quit() is not required.

Related concepts

More concepts starting with I