2.1 The Python shell
Learning objectives
By the end of this section you should be able to
- Use a Python shell to run statements and expressions interactively.
- Explain the function of the up and down arrow keyboard shortcuts.
The interpreter
Python is a high-level language, meaning that the source code is intended for humans to understand. Computers, on the other hand, understand only low-level machine code made up of 1's and 0's. Programs written in high-level languages must be translated into machine code to run. This translation process can happen all at once, or a little at a time, depending on the language.
Python is an interpreted language: the source code is translated one line at a time while the program is running. The Python interpreter translates source code into machine code and runs the resulting program. If and when an error occurs, the interpreter stops translating the source code and displays an error message.
Most development environments include a Python shell for experimenting with code interactively. A shell, also called a console or terminal, is a program that allows direct interaction with an interpreter. The interpreter usually runs an entire program all at once. But the interpreter can run one line of code at a time within a Python shell.
The arrow keys
A Python shell is convenient for exploring and troubleshooting code. The user can try something, look at the results, and then try something else. When an error occurs, an error message is displayed, but the program keeps running. That way, the user can edit the previous line and correct the error interactively.
The acronym REPL (pronounced "rep ul") is often used when referring to a shell. REPL stands for "read-eval-print loop," which describes the repetitive nature of a shell:
- Read/input some code
- Evaluate/run the code
- Print any results
- Loop back to step 1
Most shells maintain a history of every line of code the user types. Pressing the up or down arrow key on the keyboard displays the history. The up arrow displays the previous line; the down arrow displays the next line. That way, the user can repeat a line without having to type the line again.