PythonThe Python REPL

The Python REPL

Almost every Python developer's first interaction with the language happens not inside a file, but inside a little interactive prompt that echoes results back instantly. That prompt is the REPL — short for Read-Eval-Print Loop. It reads the expression you type, evaluates it, prints the result, and loops back to wait for the next one. There's no compiling, no saving, no running a separate command — you type an expression, hit Enter, and see the answer immediately.

The REPL is one of the best reasons Python is a popular teaching and prototyping language. It turns the language itself into a calculator, a scratchpad, and a debugging tool. Before reaching for a script file, experienced developers often reach for the REPL first — to check how a function behaves, to remember what a library returns, or to try out a one-line idea before committing it to a real program.

Starting the REPL

You start the REPL by running the Python interpreter from a terminal with no arguments. Depending on how Python is installed on your system, the command is either python or python3:

Bash
python3
# or, on some systems:
python

When you run this, the interpreter prints a short banner with the Python version and platform, followed by the primary prompt: >>>. This is Python telling you it's ready for input. For example:

$ python3
Python 3.12.3 (main, Apr  9 2024, 08:00:00)
[GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

From here, everything you type is Python. Try a simple expression:

>>> 2 + 2
4
>>> "hello".upper()
'HELLO'
>>> len([1, 2, 3])
3

Notice that you never typed print(). The REPL automatically prints the value of any expression that isn't already None. That's the "Print" step of Read-Eval-Print Loop happening for you, and it's what makes the REPL feel so immediate compared to running a script.

The primary prompt and the continuation prompt

The >>> you see is called the primary prompt — it appears whenever Python is ready for a brand-new, complete statement. But some constructs, like function definitions, if statements, and for loops, span multiple lines. When Python detects that a block is still open, it switches to the continuation prompt, ..., to let you know it's still waiting for more input before it evaluates anything.

Here's a multi-line function definition typed directly into the REPL. Notice the prompt changes from >>> to ... for every line that belongs to the block, and Python waits for a blank line to know the block is finished:

>>> def greet(name):
...     if name:
...         return f"Hello, {name}!"
...     else:
...         return "Hello, stranger!"
...
>>> greet("Ada")
'Hello, Ada!'
>>> greet("")
'Hello, stranger!'

This is one of the most common points of confusion for beginners: if you paste code that has inconsistent indentation, or forget the trailing blank line, the REPL can get "stuck" showing ... and refusing to run anything. If that happens, press Ctrl+C to cancel the current block and get back to a clean >>> prompt.

Recovering the last result with the underscore

The interactive interpreter has a small piece of built-in magic that scripts don't have: the special variable _ (a single underscore) always holds the result of the last expression that was evaluated and printed. It's only updated for expressions typed at the prompt — not for statements like assignments — and it only exists in the interactive REPL, not when you run a .py file.

>>> 10 * 4
40
>>> _ + 2
42
>>> _
42
>>> result = _
>>> result
42

This is handy when you compute something, look at the output, and then realize you want to do one more thing with it without retyping the whole expression. It's a small feature, but it's genuinely useful during exploratory sessions.

Note
`_` is reset the moment you evaluate a new expression, and it disappears entirely when you close the REPL. Don't rely on it for anything you need to keep — assign it to a real variable first, as shown above.
Exploring with help() and dir()

Two built-in functions make the REPL a genuine learning tool instead of just a calculator: help() and dir().

help() opens Python's interactive help system. Called with no arguments, it drops you into a help> prompt where you can look up any module, keyword, or topic by name. Called with an argument — a function, object, module, or type — it prints that object's docstring and signature directly:

>>> help(str.upper)
Help on method_descriptor:

upper(self, /)
    Return a copy of the string converted to uppercase.

>>> help(list)
Help on class list in module builtins:
...
(press q to exit the pager)

dir() is the complementary tool: instead of explaining what something does, it lists the names that are available. Called with no arguments, it lists the names defined in the current scope. Called with an object, it lists that object's attributes and methods — perfect for discovering what you can do with something you're not familiar with yet:

>>> x = "hello"
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'x']
>>> dir(x)
['__add__', '__class__', ..., 'capitalize', 'casefold', 'center', 'count',
'encode', 'endswith', 'find', 'format', 'index', 'isalpha', 'isdigit',
'join', 'lower', 'replace', 'split', 'startswith', 'strip', 'title', 'upper', ...]
Tip
A common workflow: use `dir(obj)` to see what methods an object has, then `help(obj.method)` to see what a specific one does. Together they turn the REPL into a live, always-up-to-date reference for whatever library or object you're working with — often faster than searching the web.
Exiting the REPL

There are a few equivalent ways to leave the interactive interpreter and return to your regular shell:

  • Call exit() or quit() as if they were functions (the parentheses matter — typing just exit prints a message telling you to add them).

  • Press Ctrl+D on Unix and macOS, which sends an end-of-file (EOF) signal that the REPL treats as a request to close.

  • Press Ctrl+Z followed by Enter on Windows, which is the Windows equivalent of sending EOF from the terminal.

>>> exit()
$
Warning
Everything you defined in a REPL session — variables, functions, imported modules — lives only in memory for that session. The moment you exit, all of it is gone. Unlike a saved `.py` script, there is no file to rerun later. If you build up something useful at the prompt, copy it into a real file before you close the terminal.
IPython: an enhanced REPL

The standard REPL that ships with Python is deliberately minimal, and for many quick checks that's exactly right. But for heavier interactive work, a lot of developers install IPython, a drop-in replacement REPL with a much richer feature set. You start it the same way, just with a different command:

Bash
pip install ipython
ipython

IPython keeps the same read-eval-print idea but adds tab completion for names and attributes, syntax highlighting of what you type, numbered input/output history (In [1], Out[1]), and "magic commands" prefixed with % — for example %timeit to benchmark a snippet or %run to execute a script file inside the current session. It also supports a trailing ? after any name to pull up its documentation, which is faster to type than wrapping it in help().

Feature

Standard REPL

IPython

Tab completion

Basic or none (shell-dependent)

Full, including object attributes

Syntax highlighting

No

Yes

History across sessions

Limited

Persistent, searchable

Introspection

help() / dir()

object?, object??, plus help()

Magic commands

None

%timeit, %run, %who, and more

Installation

Built in

Separate package (pip install ipython)

Neither tool is strictly better in every situation — the standard REPL is always available with zero setup, which matters when you're on a remote server or a fresh environment, while IPython is worth installing on your everyday development machine for the extra convenience. Many Jupyter notebooks and data-science tools are built directly on top of IPython's engine, so learning its conventions carries over well beyond the terminal.