Interactive Mode: Running Python Code Line by Line
Running a Python script means telling the Python interpreter to read and execute code from a file, rather than typing code interactively line by line.
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.
Provide the filename: The command tells the Python interpreter which file to use.
Open the file: The interpreter begins handling the named script file.
Read the lines: The interpreter reads the lines contained in the file.
Execute in order: Execution flows from the top of the file toward the bottom, handling each line in order.
Reach the end: After the final line has been handled, the interpreter reaches the end of the file.
Exit 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
Thinking that script execution requires entering every line interactively.
File-based execution tells the interpreter to read the code from a file rather than receiving it line by line.
Fix:
Provide the filename with python <filename> so the interpreter can read and execute the file.Expecting a script to require quit() at the end.
The interpreter automatically exits when it reaches the end of the script file.
Fix:
Allow the file-based run to end naturally when the interpreter reaches the end of the file.Ignoring execution order within the file.
Script execution flows from top to bottom, with each line executed in order.
Fix:
Trace 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
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
- Running a script means telling the Python interpreter to read and execute code from a file.
- The command for starting a script is python <filename>, entered at the operating system command prompt.
- Script execution proceeds from the top of the file to the bottom, one line after another.
- The interpreter automatically exits at the end of the file, so quit() is unnecessary in a script.
Key Takeaways
- 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.