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()orexit()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.
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 |
|
|
Returns a list of all names (attributes and methods) in the current local scope or of a specified object. |
|
|
|
Launches the built-in help system for an object, module, or function, showing its documentation (docstrings). |
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:
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.
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.
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.
Use it as a calculator or learning tool: Its immediate results make it useful for performing calculations or learning Python basics.
How to Start and Exit a REPL Session
To start a Python REPL session, open your terminal or command prompt. Then type
pythonorpython3, and press Enter. This launches the interactive shell.