Sub topic 7
Trying code in the interactive interpreter
This chapter assumes you have installed Python 3 and confirmed with python3 --version (or py --version on Windows) that the command works. You should also be able to open a terminal and type commands.
What the REPL is
The interactive interpreter is also called the REPL, short for read-eval-print loop. It reads one statement you type, evaluates it, prints the result, and waits for the next statement. Variables you create stay available until you exit, so you can build up a small experiment one line at a time.
Use the REPL to check how a function behaves, test a small expression, or look up what an object can do. Use a script file for anything you want to keep or run again.
Starting and leaving the REPL
Type the interpreter command with no file name:
python3
# On Windows, use: pyPython prints its version and a prompt, >>>. You type code after the prompt. To leave, call exit(), or press Ctrl+D on macOS and Linux or Ctrl+Z then Enter on Windows.
A first session
The session below evaluates an expression, assigns a variable, prints text, and exits. Type only what follows each >>>. Python prints the other lines.
>>> 2 + 3
5
>>> name = "Ada"
>>> print("Hello, " + name)
Hello, Ada
>>> exit()Two behaviors are worth noticing:
- An expression such as
2 + 3prints its value automatically. The REPL does this only for expressions, not for a script. - An assignment such as
name = "Ada"prints nothing. It stores a value and returns no result.
Multi-line statements
When a statement needs an indented block, the prompt changes to .... Python is waiting for the rest of the block. Press Enter on a blank line to finish it.
>>> for n in range(3):
... print(n * 2)
...
0
2
4If you see ... and do not expect it, you probably left a bracket, quote, or colon block open. Finish the statement, or press Ctrl+C to cancel it and get a fresh >>> prompt.
Useful helpers
The REPL includes a few built-in tools for exploring.
| Helper | What it does |
|---|---|
| `type(x)` | Shows the type of a value |
| `dir(x)` | Lists the names an object provides |
| `help(x)` | Shows documentation for an object |
| `_` | Holds the last printed result |
>>> type(3.5)
<class 'float'>
>>> "python".upper()
'PYTHON'
>>> len(_)
6Strings print with quotes in the REPL because it shows the representation of the value. print() shows the plain text instead.
Common errors
An error in the REPL does not end the session. Python prints a traceback and returns to the prompt, and your earlier variables are still there.
>>> print(mesage)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'mesage' is not defined
>>> 10 / 4
2.5The most frequent mistakes are:
- Typing
python3orpyinside the REPL. The prompt expects Python code, so this raises aNameError. Exit first, then run shell commands. - Pasting code that has inconsistent indentation. Blocks pasted into the REPL can behave differently from a file.
- Expecting work to be saved. The REPL forgets everything when you exit.
Exercise
In the REPL you type x = 5 and press Enter, and nothing is printed. Then you type x * 2. What does Python print, and why did the first line print nothing?
Show answer
Python prints 10. The line x = 5 is an assignment, which stores a value but produces no result to display. The line x * 2 is an expression, so the REPL prints its value.
Exercise
You type for n in range(3): and press Enter, and the prompt changes to .... What does this mean, and how do you run the loop?
Show answer
The ... prompt means Python is waiting for the rest of the indented block. Type the indented body, such as print(n), and then press Enter on a blank line to run the loop.