Informatics Practices · Ch 3 — Brief Overview of Python
Execution Modes
Execution Modes
The Python interpreter can be put to work in two distinct ways, and choosing between them is the first practical decision every programmer makes. There are two modes for running a program using the Python interpreter:
- Interactive mode
- Script mode
(A) Interactive mode
In interactive mode, a Python statement is typed directly at the >>> prompt of the interpreter. The moment the Enter key is pressed, the interpreter executes that statement and immediately displays its result(s) — Figure 3.2 shows exactly this, with statements and their answers alternating down the shell window.
This immediacy is the mode's strength. It is convenient for testing a single line of code and getting instant execution: type, press Enter, see the result. Nothing could be faster for trying out an idea.
Its weakness is permanence. In interactive mode the statements cannot be saved for future use. When the session ends, the work is lost, and running the same statements again means retyping them in full. Interactive mode is a scratchpad, not a notebook.
(B) Script mode
In script mode, the Python program is written in a file, the file is saved, and the interpreter is then used to execute the program from that file. These program files have the .py extension and are also called scripts.
The two modes therefore divide the work naturally. Beginners usually learn Python in interactive mode, because instant feedback is ideal while exploring. But for any program that runs to more than a few lines, the code should always be saved in a file so it is available for future use — it can be re-run, corrected and extended without retyping.
Creating a script
A Python script can be created using any editor. Python comes with a built-in editor of its own, IDLE — the Integrated Development and Learning Environment. The steps in IDLE are:
- Open IDLE.
- Click File > New File to create a new file.
- Write the program in that file.
- Save it with a desired name. By default, Python scripts are saved in the Python installation folder.
Figure 3.3 shows a short program saved this way as prog3-1.py.
Executing a script
To execute a Python program in script mode:
- Open the program using an editor — for example IDLE, as shown in Figure 3.3.
- In IDLE, go to [Run] -> [Run Module] to execute the program (
prog3-1.py), as shown in Figure 3.4. - The output appears on the shell, as shown in Figure 3.5. …
The figure is a screenshot of the Python 3.7.0 Shell window captured in the middle of an interactive session — the interpreter being used exactly as interactive mode intends, with statements typed at the >>> prompt and answers appearing at once.
Three statements and their results are visible, one after another down the window. First, 1 + 2 has been typed at the prompt, and the interpreter has replied on the next line with 3. Second, 4 - 2 has been entered, answered immediately with 2. Third, "Hello" + "World" has been typed, and the interpreter has displayed 'HelloWorld' — the two pieces of text joined into one. After the third result the window ends at a fresh, empty >>> prompt, showing the interpreter once again waiting for the next instruction.
The session it depicts would read like this:
>>> 1 + 2
3
>>> 4 - 2
2
>>> "Hello" + "World"
'HelloWorld'
``` …
The figure is a screenshot of a program open in IDLE's editor window — the script-mode side of Python, where code lives in a file rather than at the interactive prompt. The title bar reads prog3-1.py - C:\NCERT\prog3-1.py (3.7.0), which packs in three facts at once: the file's name (prog3-1.py, carrying the .py extension that marks a Python script), the exact location where it is saved on disk (the C:\NCERT folder), and the Python version in use (3.7.0).
Below the title bar runs the editor's menu bar with the entries File, Edit, Format, Run, Options, Window and Help. Two of these matter most to a beginner: the File menu, through which a new file is created (File > New File) and saved, and the Run menu, through which the saved program is executed.
The body of the window contains the program itself — just two lines of code:
print("Save Earth")
print("Preserve Future")
``` …
The figure is a screenshot of the same IDLE editor window holding prog3-1.py, but now captured at the decisive moment of script mode: the Run menu has been opened from the menu bar, showing how a saved program is actually executed.
The opened Run menu lists three items. Python Shell brings up the interpreter window itself. Check Module, with the keyboard shortcut Alt+X beside it, is available for checking the module. The third item, Run Module, with the shortcut F5, is shown highlighted — this is the command being chosen, and it is the one that executes the program in the file. The highlight is the whole point of the screenshot: of everything in the menu, [Run] -> [Run Module] is the action that turns a saved script into a running program. …
The figure is a screenshot of the Python 3.7.0 Shell window immediately after prog3-1.py has been executed in script mode — the "after" picture that completes the sequence begun by the editor screenshots before it.
At the top of the window sits the interpreter's usual version banner identifying Python 3.7.0. Below it appears a separator line reading RESTART: C:\NCERT\prog3-1.py — the shell's marker that it has restarted and is now running the named file from disk. This line is the visible link between the two windows: it names the very script that was open in the editor, complete with its folder path.
Directly under the restart line comes the program's output, exactly the two lines its two print statements produce:
Save Earth
Preserve Future
After the output, the window ends at a >>> prompt, the interpreter signalling that the program has finished and the shell is ready for further instructions. …