Concepts / Organizing Code with Functions and Modules

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.

  • Programming

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.

acceptsevaluatesprovidesmay displayInteractiveinterpreterimmediate feedbackCommandentered at promptResultshown immediatelyPython script.py fileExecutionruns in orderOutputonly when printed
What is the practical difference between entering Python interactively and running code saved in a script?

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.

save asrunfinish or stopText editorwrite Python codePython file.pyPython interpreterread and executeOperating systemcontrol returns
How does Python code move from being written in an editor to being executed by the interpreter?

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.

readthencheck executionno errorerror foundScript filebeginningFirst instructionexecuteNext instructioncontinue in orderErrorstop and reportCompletionreturn control
What happens next when the Python interpreter runs each line of a saved script?

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?

  • The interpreter shows every result automatically
  • The script runs silently
  • The interpreter waits for a new command after every instruction
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

SituationBetter choiceReason
Learning a featureInteractive interpreterImmediate feedback supports experimentation
Testing a small piece of codeInteractive interpreterA command can be entered and evaluated immediately
Exploring a function or moduleInteractive interpreterThe environment is useful for quick experiments
Building a complete programPython scriptThe code can be saved, organized, and run again
Solving a complex repeated problemPython scriptThe instructions do not need to be retyped
Creating a program others will usePython scriptThe 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

EASY

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.
  1. 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.