Understanding Python Source Files
There are two ways of using Python to run your program - using the interactive interpreter prompt or using a source file. We will now see how to use both of these methods.
Two Places for Python Code
Python programs can be run in two ways: by using the interactive interpreter prompt or by using a source file. The important difference is where your program instructions begin. With the interactive approach, you work at Python's prompt. With the source-file approach, you first type and save the instructions in a file.
Interactive Prompt and Source File
The diagram shows the shared purpose of both approaches: they are both ways of using Python to run a program. The starting point differs. The interactive workflow begins with code entered at the interpreter prompt. The source-file workflow begins with code typed and saved in a file.
| Interactive interpreter prompt | Python source file |
|---|---|
| Python code is entered at the interactive prompt. | Python code is typed and saved in a source file. |
| The prompt is the starting place for using Python directly. | The saved file is the starting place for using Python through a source file. |
| It is one of the two ways to run a program. | It is the other way to run a program. |
Building a Source File
Creating a Python source file requires editor software where you can type and save. The editor is therefore part of the source-file workflow: it provides the place to write the program and preserve it as a file.
- Open editor software suitable for writing a source file.
- Type the Python program in the editor.
- Save the typed program as a source file.
- Use the source file as the file-based way to run the Python program.
Following the source-file workflow
A learner wants to use Python through a saved source file rather than entering the program at the interactive interpreter prompt. What sequence should the learner follow?
Choose an editor: The learner uses editor software because a source file must be created by typing and saving.
Type the program: The program instructions are entered in the editor.
Save the file: Saving turns the typed instructions into the source-file part of the workflow.
Run through the source-file approach: The saved source file is used for the file-based way of running the Python program.
The learner has followed the source-file workflow rather than the interactive-interpreter workflow.
A source file is not created merely by thinking about a program or entering it at the prompt. In the workflow described here, you use an editor to type the program and save it.
Choosing an Editor
The choice of editor matters when you create Python source files. The source material describes a good programmer's editor as something that can make writing source files easier and make the programming journey more comfortable, faster, and safer.
Mistakes in the Workflow
Treating the interactive interpreter prompt and a source file as the same thing.
The material identifies the interactive interpreter prompt and the source file as two different ways of using Python to run a program.
Fix:
Remember that the prompt is one workflow, while the source-file workflow uses editor software to type and save the program.Skipping the editor when creating a source file.
The source material states that editor software is needed to create Python source files.
Fix:
Use an editor to type the program and save it as a source file.Assuming that choosing any editor is an unimportant decision.
The source material emphasizes that the choice of editor is crucial and that a good programmer's editor can make writing source files easier.
Fix:
Choose an editor with the goal of making source-file writing more comfortable, faster, and safer.
Check Your Understanding
Explain the difference between these two situations: entering Python code at the interactive interpreter prompt, and typing and saving Python code in an editor as a source file. Then list the steps a learner follows when creating a source file.
Hints
- Start by naming the two ways Python can be used to run a program.
- For the source-file workflow, include the editor, typing, and saving.
- Do not describe the interactive prompt as if it were the saved source file.
Key Takeaways
- Python can be used to run a program through the interactive interpreter prompt or through a source file.
- The interactive prompt and the source-file workflow are distinct ways of using Python.
- Creating a source file involves using editor software to type and save the program.
- Choosing a suitable editor can make writing source files easier, more comfortable, faster, and safer.
Key Takeaways
- Python programs can be run through the interactive interpreter prompt or through a source file.
- A source file is created by typing and saving program instructions in editor software.
- The editor is an important part of the source-file workflow.
- The interactive prompt and a saved source file should be understood as separate workflows.