Organizing Code with Functions and Modules
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
Python code can be entered directly into the interactive interpreter or saved in a script. The interactive interpreter is designed for immediate experimentation: you type a command, press Enter, and see the result. A script is better suited to a complete program that you want to save, organize, and run again.
What a Script Is
A Python script is a text file containing Python code, saved with the .py extension, that the interpreter executes from top to bottom.
A script must be saved as plain text. A simple text editor, a code-focused editor, or an integrated development environment can be used, provided that the file is saved as plain text rather than as a formatted document. The .py extension identifies the file as a Python source file.
Execution from Top to Bottom
When a saved script is run, the interpreter opens the file and reads the code from the beginning to the end. It then executes each instruction in order, as if the instructions had been entered one by one into the interactive interpreter. If an error occurs, execution stops and the interpreter reports the problem. If the script finishes successfully, the interpreter exits and returns control to the operating system.
What do you think happens?
A saved script completes successfully but does not explicitly use print(). What should you expect to see while it runs?
Reveal answer
Answer: The script runs silently.
Interactive mode shows results immediately, but script mode provides no interactive feedback during execution unless the script explicitly tells Python to display output.
Why Larger Problems Need Scripts
A Repeated Data-Analysis Task
Imagine a program that analyzes data from a hundred files, performs calculations, and generates a report.
Try the interpreter: Entering every step interactively would be tedious and error-prone. The commands would also need to be retyped if a mistake occurred or if the task had to be repeated later.
Save the work: A script preserves the Python code in a .py text file, so the program can be run again without re-entering every instruction.
Organize the program: The script allows the code to be organized logically and comments to be added to explain what each section does.
For a repeated or complex task, a script is more practical than a sequence of one-off interpreter commands because it is persistent, reusable, and organized.
Scripts become increasingly important as a program grows. They preserve the work after the interpreter is closed, make it possible to run the program again, and provide a place to organize code logically. This is why scripts are not merely a convenient alternative to the interpreter; for complex problems, they are essential.
Start an experiment in the interactive interpreter when you need immediate feedback. Move the work into a saved script when it becomes a complete solution, needs to be repeated, or should be available to other people.
Choosing Between Interpreter and Script
| Situation | Better choice | Reason |
|---|---|---|
| Learning a feature | Interactive interpreter | Immediate feedback supports experimentation |
| Testing a small piece of code | Interactive interpreter | A command can be entered and evaluated immediately |
| Exploring a function or module | Interactive interpreter | The environment is useful for quick experiments |
| Building a complete program | Python script | The code can be saved, organized, and run again |
| Solving a complex repeated problem | Python script | The instructions do not need to be retyped |
| Creating a program others will use | Python script | The program can be preserved as a reusable file |
Using the interactive interpreter as the permanent home for a large program.
The interactive interpreter is designed for one-off commands, and the code disappears when the interpreter is closed.
Fix:
Save substantial or reusable work in a Python script.Expecting a script to show results automatically.
Scripts run silently unless output is explicitly requested with print().
Fix:
Use explicit output instructions when the script needs to display information.Treating an error as if the rest of the script will continue automatically.
The interpreter stops when an error occurs.
Fix:
Read the reported problem and correct the script before running it again.Saving Python code as a formatted document.
A Python script is a plain-text file containing Python code.
Fix:
Use an editor that saves plain text and use the .py extension.
Practice and Takeaways
For each situation, decide whether the interactive interpreter or a saved Python script is the better choice: exploring how a feature works, analyzing a hundred files and generating a report, and building a program that should be run again tomorrow.
Hints
- Look for immediate experimentation versus persistence and repetition.
- A task that must be repeated should not depend on retyping one-off commands.
- Remember that a script is saved as a plain-text .py file.
- The interactive interpreter is a sandbox for quick experiments and immediate feedback. A Python script is a plain-text file containing Python code and saved with the .py extension. When a script runs, the interpreter reads it from beginning to end and executes its instructions in order. An error stops execution, while successful completion returns control to the operating system. Scripts are essential for complex work because they preserve, reuse, and logically organize programs.
Key Takeaways
- Use the interactive interpreter for learning, exploration, and small experiments that benefit from immediate feedback.
- Use a script for a complete, reusable, or complex program that must be saved and run again.
- A Python script is a plain-text file containing Python code and normally uses the .py extension.
- The interpreter reads a script from top to bottom, stopping if an error occurs.
- Scripts run silently unless the code explicitly uses print() to display output.