REPL: The Power of the Python Interactive Shell
REPL stands for Read, Eval, Print, and Loop. It represents the core cycle of the Python language shell. This guide provides a deep dive into using the Python Interactive Shell and helps beginners and intermediates in their Python learning journey.

Understanding REPL

When you dive into Python programming, you’ll encounter the Python Interactive Shell, commonly known as REPL. The acronym details a four-step process:

  1. Read: It takes user input.
  2. Eval: It evaluates or processes the input.
  3. Print: It displays the result or output to the user.
  4. Loop: It goes back to the first step, waiting for more input.

Related Course: Complete Python Programming Course & Exercises

Getting Started with REPL

Before diving into REPL, it’s important to check which version of Python you’re running. Simply type python and, instead of hitting enter, press the tab key. Your system will display all installed versions of Python:

➜  ~ python
python python2.7 python3.7 python3.7m python3-config python3m python3-pasteurize
python2 python3 python3.7-config python3.7m-config python3-futurize python3m-config python3-wsdump

To drill down on a specific version, use:

python --version

It’s recommended to use Python 3 or newer since Python 2 is considered legacy.

Launching the Interactive Shell

Launching REPL is straightforward. Begin by opening a terminal or command prompt, then type python followed by enter.

Upon initiation, Python will provide version details:

$ python 
Python 3.7.5 (default, Nov 20 2019, 09:21:52)
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

The symbols >>> represent the prompt where you can enter Python commands. Each command will be evaluated. For instance:

>>> hello world
File "<stdin>", line 1
hello world
^
SyntaxError: invalid syntax
>>>

In the example above, the syntax error indicates that text strings should be wrapped in quotes:

>>> "hello world"
'hello world'
>>>

Python Interactive Shell REPL

Users with certain versions of Ubuntu might need to type python3 instead of python.

Inside the Python shell, it’s possible to perform various operations, even use it as a simple calculator:

>>> 128 / 8
16.0
>>> 256 * 4
1024
>>>

Storing and retrieving data using variables is also achievable in the Python shell:

>>> width = 10
>>> height = 20
>>> area = width*height
>>> print(area)
200
>>>

Pro Tip: Our next article covers executing complete Python programs. Stay tuned!

Exiting the Interactive Shell

Want to leave the Python Interactive Shell? There are a few methods. One approach is typing the command exit(), ensuring you include the brackets:

>>> exit()
➜ ~

Alternatively, the keyboard shortcut Ctrl-D will achieve the same result.

Dive deeper into Python with these recommended reads: If you are a Python beginner, then I highly recommend this book.
Download exercises