1.5.2: IDLE Programming Environment: Notes on Python Interpreter, Interactive and Script Modes
Python Interpreter and Modes
Python is an interpreted language. The Python interpreter can run programs saved in files or execute statements interactively typed at the keyboard.
Python comes with IDLE, an integrated development environment (IDE) that simplifies writing, executing, and testing programs.
When Python is installed, the Python interpreter is installed as well.
The interpreter can operate in two modes: interactive mode and script mode.
Interactive mode: the interpreter waits for you to type Python statements on the keyboard, executes each statement, and then waits for another input.
Script mode: the interpreter reads a file that contains Python statements (a Python program or Python script) and executes each statement as it reads it.
Starting the interpreter in interactive mode
After installation, start the interpreter from the operating system’s command line by typing:
python.If you are on Windows, you can alternatively type
Pythonin the Windows search box; you may see a program named something likePython 3.5(the indicates the installed version). denotes the version referenced here as the latest at the time of writing.Clicking this item starts the Python interpreter in interactive mode (the Python shell).
When the interpreter starts in interactive mode, you typically see a console display like:
Python 3.x.y(an example given isPython 3.9.5).The prompt you see is the three chevrons
>>>, which indicates the interpreter is waiting for you to type a Python statement.A simple first action is to print a message to the screen.
Interactive mode basics and examples
Example: print a message on the screen
Code:
print('Python programming is fun!')This prints:
Python programming is fun!to the screen.After executing, the interpreter shows the prompt again, waiting for another statement.
You can try another example, e.g.:
print('That is the question?')
If you type a statement incorrectly, the interpreter displays an error message. This immediate feedback makes interactive mode useful for learning.
Quitting interactive mode:
Windows: press
CTRL+Zand then Enter.macOS/Linux: press
CTRL+D.
In Chapter 2 of the book, additional details about these statements are discussed.
Script mode: writing and running programs
Interactive mode is great for testing, but the statements you enter there are not saved anywhere. To save a Python program, you must write statements to a file.
Use a plain text editor (e.g., Notepad on Windows) to create a file containing Python statements.
Save the file with a
.pyextension, which identifies it as a Python program. Example:test.py.To run the program, navigate to the directory where the file is saved and type on the command line:
python test.py
This starts the Python interpreter in script mode and executes the statements in the file in order. When the program finishes, the interpreter exits.
Example program (three lines) saved as a file
A Python program that displays three lines of text:
print('nudge, nudge')print('wink, wink')print('know what I mean?')
Running this script with
python test.pywill print:nudge, nudgewink, winkknow what I mean?
Notes on editing and file types
It is possible to use a word processor to create a Python program, but you must save the program as plain text; otherwise the Python interpreter cannot read its contents.
Save files as plain text with the extension
.pyto identify them as Python programs (the example name:test.py).The phrase “the Python interpreter in script mode” refers to running a Python program from a file rather than typing commands interactively.
Quick reference and practical considerations
Start interactive: from the OS command line, run
python(on Windows you may see a listing likePython 3.5in search results to start the interactive shell). The version shown may be represented as (latest at writing).Start script mode: save a file as
name.pyand runpython name.py.Quit interactive mode:
CTRL+Z(Windows) orCTRL+D(macOS/Linux).File extension:
.py.
Context and broader relevance
The interpreter works by reading and executing statements; interactive mode is ideal for experimentation and learning, while script mode enables running complete programs and sharing them as files.
IDLE is a beginner-friendly environment that helps writing, executing, and testing Python programs.
Appendix A (referenced in the material) provides installation instructions for Python on personal computers; in computer labs, Python is often pre-installed.
The content also mentions the distinction between Python being an interpreted language and the practical workflow of writing and testing code before saving it as a script.
Mathematical and numerical notes (versions and references)
Latest version at time of writing:
Example interactive shell display:
Python 3.x.y(e.g., )
Summary
You can run Python in two modes: interactive and script.
Interactive mode is great for quick experiments and learning; script mode is for saving and running complete programs.
IDLE provides a friendly environment for beginners.
To save and run programs, use a text editor to create a
.pyfile and run it withpython <filename.py>.The basic print example demonstrates how Python executes statements and provides immediate output in the interactive shell.