Understanding the Python Interpreter
A Python script is a text file containing Python code, saved with the .py extension, that the interpreter executes from top to bottom.
Two Ways to Run Python
When you work with Python, you can enter instructions directly into the interactive interpreter or save instructions in a Python script. These approaches use the interpreter differently. Interactive mode is designed for immediate experimentation, while a script is designed for a complete program that can be saved, organized, and run again.
Use the interactive interpreter for quick experiments and immediate feedback. Use a script when your work should be persistent, reusable, organized, or repeatable.
Following a Script Through the Interpreter
A Python script is a text file containing Python code. It is saved with the .py file extension. When you run the script, the interpreter opens the file, reads the code from the beginning to the end, and executes each instruction in order. This is similar to entering the instructions one by one into the interactive interpreter, except that the instructions have already been saved together in a file.
The important sequence is file, reading, execution, and completion or error. If an error occurs, the interpreter stops and reports the problem. If every instruction completes successfully, the interpreter exits and returns control to the operating system.
Creating and Running a Script
From Editor to Execution
Trace the workflow for creating a Python script that can be run again later.
Write: Use a text editor to write Python code. The editor may be a simple program, a code-focused editor, or an integrated development environment.
Save: Save the code as a plain-text file with the .py extension. Formatted documents such as Word files are not the required file type.
Run: Run the saved file with the Python interpreter. The interpreter opens the file and reads its code from the beginning to the end.
Execute: The interpreter executes the instructions in order. If an error occurs, execution stops and the problem is reported.
Repeat: Because the code remains saved in the script, the program can be run again without retyping all of its instructions.
A script turns written Python instructions into a persistent program that the interpreter can execute and run again.
Interactive Work and Scripted Work
The interactive interpreter behaves like a sandbox for learning and exploration. You type a command, press Enter, and immediately see the result before entering another command. This makes it useful for experimenting with individual statements, testing how a function behaves, or exploring Python's built-in features.
A script has a different workflow. The code is saved before it is run, and there is no prompt waiting for the next command during execution. A script runs silently unless you explicitly use print() to display output. This means that an instruction can execute without automatically showing its result.
| Interactive interpreter | Python script |
|---|---|
| Commands are entered one at a time | Instructions are saved together in a file |
| Provides immediate feedback | Runs without a prompt during execution |
| Useful for experimentation | Useful for complete, reusable programs |
| Code disappears when the interpreter is closed | Code remains in the saved .py file |
| Results are shown automatically in the interactive workflow | Output must be explicitly requested with print() |
Why Complex Problems Need Scripts
The interactive interpreter is effective for a small experiment, but it becomes impractical when a task contains many steps. Consider a program that analyzes data from a hundred files, performs calculations, and generates a report. Entering every step manually would be tedious and error-prone. If you made a mistake or wanted to run the task again later, you would have to retype the instructions.
Scripts solve this problem by keeping the instructions available. They allow you to organize code logically, add comments that explain what sections do, and run the entire program reliably without retyping it. As a program becomes more complex, these qualities make scripts essential rather than merely convenient.
Choosing the Right Workflow
Matching Tasks to Tools
Choose whether the interactive interpreter or a Python script is better for each task.
Testing a function: Choose the interactive interpreter because immediate feedback helps you explore how the function behaves.
Trying a small Python feature: Choose the interactive interpreter because it is suited to quick experimentation.
Building a complete program for others: Choose a script because scripts are suited to complete, reusable programs.
Analyzing data from many files repeatedly: Choose a script because the instructions need to be organized, saved, and run again without retyping.
Immediate experimentation belongs in the interactive interpreter. Persistent, organized, and repeatable work belongs in a script.
For each situation, decide whether the interactive interpreter or a Python script is the better choice. Explain why: exploring a built-in feature, creating a program that another person will use, testing one small statement, and running the same multi-step task again tomorrow.
Hints
- Look for situations that need immediate feedback.
- Look for situations that need persistence, organization, or repeated execution.
- Remember that a script is saved as a plain-text .py file.
Mistakes Beginners Make
Treating the interactive interpreter as a permanent program file
The interactive interpreter is intended for one-off commands, and the code disappears when it is closed.
Fix:
Move work that must be kept, reused, or run again into a saved Python script.Expecting a script to show every result automatically
A script runs silently unless output is explicitly requested with print().
Fix:
Use explicit output when the script needs to display information.Saving Python code as a formatted document
The required workflow uses a text file containing Python code, not a formatted document.
Fix:
Use a text editor, code-focused editor, or integrated development environment that saves plain text, and use the .py extension.Retyping a complex task every time it must run
Manual re-entry is tedious, error-prone, and difficult to reproduce reliably.
Fix:
Organize the instructions in a script so the saved program can be run again.
Key Takeaways
- A Python script is a plain-text file containing Python code and saved with the .py extension.
- The interpreter reads a script from the beginning to the end and executes its instructions in order.
- If an error occurs during script execution, the interpreter stops and reports the problem.
- The interactive interpreter provides immediate feedback and is useful for short experiments and exploration.
- Scripts are persistent, reusable, and organized, making them suitable for complete programs and complex repeatable tasks.
- Scripts run silently unless output is explicitly requested with print().
Key Takeaways
- Use the interactive interpreter for quick experiments, immediate feedback, and exploring small pieces of Python.
- Use a script for a complete program or any work that must be saved, organized, reused, or repeated.
- A script is a plain-text Python file with the .py extension.
- The interpreter reads and executes script instructions from top to bottom, stopping and reporting an error if one occurs.
- A script does not automatically display every result; output must be explicitly requested.