Using the Python REPL

Share
Copied to clipboard.
Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
4 min. read Watch as video Python 3.8—3.12

Let's talk about the interactive Python interpreter, also known as the Python REPL.

Also see the Python REPL definition in Python Terminology.

Also try out the Online Python REPL if you're looking for an in-browser REPL.

The interactive Python interpreter

If you launch Python from your system command prompt without specifying any Python program to run, you'll end up in the interactive Python interpreter:

$ python3
Python 3.12.0 (main, Oct  3 2023, 08:54:27) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

The interactive interpreter will accept a Python statement and immediately run that statement:

>>> x = 4

If the statement we're running has a result, that result will be printed out automatically:

>>> x + 2
6

Launching the interactive Python interpreter

To launch the interactive interpreter, you can usually type python3 or just python on Linux and Mac:

$ python
Python 3.12.0 (main, Oct  3 2023, 08:54:27) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Though, on Windows you'll sometimes need to type py instead:

C:\>py
Python 3.12.0 [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

Note that we're typing these commands from a terminal window on Mac or Linux or a command prompt window on Windows.

What is the Python REPL?

The interactive Python interpreter goes by many names.

Sometimes you'll hear it referred to as the Python prompt, the Python console, or the Python shell, but it's most often called the Python REPL.

REPL is an acronym. It stands for Read, Evaluate, Print, and Loop.

Those four words describe how the REPL works:

  1. It reads what we've typed
  2. Then it evaluates it, meaning it runs the code
  3. Then it prints any value that's returned to it
  4. Then it loops (to step 1), meaning it waits for more input

REPL is just an unofficial term, but it's probably the most commonly used term for the interactive Python interpreter.

IDLE, Jupyter, and other Python interpreters

The Python REPL that we've just seen isn't the only way to launch an interactive Python interpreter.

Python also includes a graphical interpreter called IDLE, which runs in its own window outside of the terminal window:

$ python3 -m idlelib

There are also many third-party interpreters, like IPython and Jupyter Notebooks:

$ ipython
Python 3.10.3 (main, Mar 19 2022, 22:08:03) [GCC 9.4.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.5.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]:

But all of these interpreters work a little bit differently. I mostly use the default Python interpreter, because that's the one I'm used to.

Use the Python REPL as a playground

You can think of the Python REPL like a playground. You can use the REPL to play with Python.

After you learn something new, you can try out that new skill by typing some code at the REPL. As we type our code, Python will wait for us to hit the Enter key, and then it evaluates the statement that we just gave it:

>>> when = "today"
>>>

If we run a statement that has a result, Python will print out that result:

>>> f"Hey! I learned something new {when}!"
'Hey! I learned something new today!'

The REPL works line-by-line

You might have noticed that the REPL shows three greater-than signs (>>>) at the beginning of every line. Those greater-than signs let us know that we're in the Python REPL:

>>>

But there's one other prompt that we might see at the beginning of each line.

Take a look at what happens if we start typing a for loop, and then we hit Enter:

>>> veggies = ["carrots", "jicama", "cucumbers"]
>>> for vegetable in veggies:
...

We now see three periods (...) instead of three greater-than signs.

That means that Python knows we're not done. It's waiting for us to type more.

Python does this when we're inside a block of code, a multi-line string, or unclosed brackets or braces (thanks to implicit line continuations).

Let's type another line, and then we'll hit Enter:

>>> veggies = ["carrots", "jicama", "cucumbers"]
>>> for vegetable in veggies:
...     print("I like", vegetable)
...

We see three dots again. But why?

Well, normally Python ends a block of code based on indentation. So when we're done with a block of code, we just outdent the code.

But at the Python REPL, there's no way for Python to know that we're done with a block of code, because there is no next line of code, since at the REPL, we're writing the code as we go.

So to end a block of code at the Python REPL, we have to hit Enter two times:

>>> veggies = ["carrots", "jicama", "cucumbers"]
>>> for vegetable in veggies:
...     print("I like", vegetable)
...
I like carrots
I like jicama
I like cucumbers

That's the biggest difference between the Python REPL and the Python code within a text file.

The REPL is great for testing out code

If you see three greater than signs (>>>) at the beginning of your terminal's prompt, then you are in the Python REPL. The Python REPL is not a great place to write full Python programs because it doesn't save your work. But the REPL is great for writing a little bit of code and then immediately seeing the result of running that code.

Concepts Beyond Intro to Python

Intro to Python courses often skip over some fundamental Python concepts.

Sign up below and I'll share ideas new Pythonistas often overlook.