LESSON · 1 OF 5

The Python REPL

The Python REPL

Run python3 with no filename and you land in the REPL - an interactive prompt where you type one line of Python and see the result straight away. REPL stands for Read, Evaluate, Print, Loop. It reads what you type, runs it, prints the result, and waits for the next line.

Starting and leaving it

Type python3 and press Enter. The prompt changes to >>>, which means Python is waiting for you:

text
$ python3
Python 3.12.3
>>> 2 + 2
4
>>> "web-01".upper()
'WEB-01'
>>>

Notice you did not have to print anything. In the REPL, the value of whatever you type is echoed back automatically. To leave, type exit() and press Enter, or press Ctrl and D together.

What the REPL is good for

The REPL is a scratchpad. It shines when you want a quick answer without writing a file:

  • Checking what a function does before you commit to it.
  • Testing a string method on a sample value.
  • Doing quick arithmetic or unit conversions.
  • Poking at a library to see what it returns.

Reach for it whenever you catch yourself wondering "what would this line actually do".

The last-result shortcut

In the REPL, a single underscore holds the value of the last expression. It saves retyping:

text
>>> 1024 * 8
8192
>>> _ / 1000
8.192

What the REPL is not for

Anything you type in the REPL is gone once you close it. There is no file to save, re-run, or hand to a teammate. The moment you want to keep code and run it again, put it in a script file, which is the next node.

Spin up a fresh environment and practice live.
python-devops · fresh machine · ready in under a minute