Skip to main content

Python REPL: A Hands-On Guide to Interactive Coding

Learn how Python’s REPL lets you run code interactively for testing, debugging, and learning. Discover essential commands, customization options, and advanced alternatives.
· 10 min read

Explore with AI

Unlike compiled languages such as C++ or Java, which must be translated into machine code before execution, Python is interpreted and runs instructions directly. This makes the REPL environment a natural fit for experimentation, whether you’re testing snippets, exploring new concepts, or debugging line by line. Its advantage is that it comes built into every Python installation, so it’s ready whenever you are.

In this tutorial, I will show you how to use the Python REPL environment using examples, explore its features, and alternative REPLs to improve your workflow. If you are getting started in Python, I recommend taking our Introduction to Python course, which covers skills like data types, lists, basic functions, and packages.

What Is a Python REPL?

The REPL session stands for Read-Eval-Print Loop, describing its four fundamental stages:

  • Read: The REPL environment reads the user's input, which can be a single line of code or a multi-line statement.
  • Evaluate: It evaluates the code, executes the statement or expression, and calculates its result.
  • Print: This function prints the evaluation result to the console. If the code doesn't produce an output, like an assignment statement, it doesn't print anything.
  • Loop: The REPL loops back to the start, ready for the next line of input.

These stages work together continuously, creating an interactive environment where you can type code and see the output instantly. Python’s REPL presents a primary prompt >>> for new statements and a secondary prompt ... for continued lines, such as inside loops or function definitions. Using examples in the sections below, we will look at how these prompts work.

The terms "Interactive shell," "interpreter session," and "REPL" are often used interchangeably to describe this environment.

Why Use a Python REPL

You can use a Python REPL for the following tasks:

Quickly test code snippets and library functions: You can try small pieces of code instantly without creating a script file.

>>> import math
>>> math.sqrt(16)
4.0

Explore syntax and experiment with ideas: Think of the REPL environment as your personal sandbox. You can try out new things, such as different data types, operators, and control flow structures, to see exactly how they behave without any risk of messing up a bigger program.

>>> [x**2 for x in range(5)]
[0, 1, 4, 9, 16]

Debug and inspect variables: You can use the REPL session to check the values and states of your variables step-by-step, making it much easier to understand and troubleshoot issues.

>>> x = 42
>>> type(x)
<class 'int'>
>>> x / 7
6.0

Use it as a calculator or learning tool: Its immediate results make it useful for performing calculations or learning Python basics.

>>> 10 * (5 + 2)
70

How to Start and Exit a REPL Session

To start a Python REPL session, open your terminal or command prompt. Then type python or python3, and press Enter. This launches the interactive shell.

You can also start an interactive session after running a script using the -i flag command. For example, python -i calc_tax.py will execute calc_tax.py and then drop you into the REPL environment with the script's variables still loaded in memory.

To exit the REPL, you can use one of several commands:

  • Type quit() or exit()  then press Enter.

  • On Unix/Linux/macOS, press Ctrl+D.

  • On Windows, press Ctrl+Z and then press Enter.

Running Code in the REPL Environment

You can run code in Python’s REPL sessions in different ways depending on its complexity. Let’s look at the following type of code you can run in REPL.

Simple statements and expressions

The REPL environment can evaluate simple expressions and statements instantly. For example, you can perform arithmetic operations, assign variables, and evaluate Boolean expressions. The result of an expression is automatically printed. 

>>> 5 + 4
9
>>> name = "Allan"
>>> name
'Allan'
>>> 10 > 5
True

If a statement, like a variable assignment, doesn't produce an output, the REPL interpreter doesn't print anything. If you make a mistake, the REPL provides immediate feedback by raising an error.

Compound statements

Python REPL environment also handles multi-line compound statements like if statements and for loops. It automatically switches to the secondary prompt (...) after you type a line ending in a colon (:) and press Enter, indicating it's waiting for the indented block of code.

Take note that you'll need to indent your code manually; the REPL environment does not auto-indent. To signal the end of the compound statement, press Enter on an empty line.

Explicit and implicit line continuations

You can also write multi-line statements in Python REPL session using line continuations. An explicit line continuation uses a backslash \ at the end of a line to indicate the statement continues on the next line. 

Implicit line continuations occur automatically when the code is enclosed in parentheses (), brackets [], or braces {}.

Printing vs evaluating

The REPL's auto-printing feature only works on expressions with a return value. For example, if you have a list and append an item using temp.append(45), it will not produce any output. To see the new list, you need to explicitly evaluate the list variable on the next line. For the functions that return None. You need to use print() to see their output.

Useful REPL Features

The Python REPL environment has several built-in features that make it more productive to use and learn. I will discuss some of these features I have found useful.

Special variable _

The underscore (_) is a special variable in the REPL session that stores the result of the last expression. This is useful when you want to reuse results without retyping them.

In the example below, _ stores the value 30, which results from the expression 6*5.

Introspection tools

Python provides several built-in functions for interactively exploring objects and your environment. The table below summarizes the use of each function with examples.

Tool

Purpose

Example

dir()

Returns a list of all names (attributes and methods) in the current local scope or of a specified object.

>>> dir() or >>> dir(list)

help()

Launches the built-in help system for an object, module, or function, showing its documentation (docstrings).