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.
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.
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.
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.
A Top-to-Bottom Trace
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.
- 1Provide the filename
The command tells the Python interpreter which file to use.
- 2Open the file
The interpreter begins handling the named script file.
- 3Read the lines
The interpreter reads the lines contained in the file.
- 4Execute in order
Execution flows from the top of the file toward the bottom, handling each line in order.
- 5Reach the end
After the final line has been handled, the interpreter reaches the end of the file.
- 6Exit automatically
The interpreter exits when it reaches the end of the file, so the script does not need a
quit()command.
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.
Mistakes to Avoid
Common mistake Thinking that script execution requires entering every line interactively.
What it looks likeTreating a script run as though each line must be typed separately at the command prompt.
Why it failsFile-based execution tells the interpreter to read the code from a file rather than receiving it line by line.
Do this insteadProvide 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 likeAdding quit() simply to stop execution after the final line.
Why it failsThe interpreter automatically exits when it reaches the end of the script file.
Do this insteadAllow 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 likeAssuming the interpreter can execute the file's lines in an arbitrary order.
Why it failsScript execution flows from top to bottom, with each line executed in order.
Do this insteadTrace a script from its first line through its final line.
Check Your Understanding
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?
Reveal 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
- 1Running a script means telling the Python interpreter to read and execute code from a file.
- 2The command for starting a script is python <filename>, entered at the operating system command prompt.
- 3Script execution proceeds from the top of the file to the bottom, one line after another.
- 4The 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.