Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Getting the Most Out of the Python Standard REPL
The Python standard shell, or REPL (Read-Eval-Print Loop), allows you to run Python code interactively while working on a project or learning the language. This tool is available in every Python installation, so you can use it at any moment.
As a Python developer, you’ll spend a considerable part of your coding time in a REPL session because this tool allows you to test new ideas, explore and experiment with new tools and libraries, refactor and debug your code, and try out examples.
In this tutorial, you’ll learn how to:
- Run the Python standard REPL, or interactive shell
- Write and execute Python code in an interactive session
- Quickly edit, modify, and reuse code in a REPL session
- Get help and introspect your code in an interactive session
- Tweak some features of the standard REPL
- Identify the standard REPL’s missing features
You’ll also learn about available feature-rich REPLs, such as IDLE, IPython, bpython, and ptpython.
To get the most out of this tutorial, you should be familiar with your operating system’s command line, or terminal. You should also know the basics of using the python command to run your code.
Free Sample Code: Click here to download the free sample code that you’ll use to explore the capabilities of Python’s standard REPL.
Getting to Know the Python Standard REPL
In computer programming, you’ll find two kinds of programming languages: compiled and interpreted languages. Compiled programming languages like C and C++ will have a compiler program, which takes care of translating the language’s code into machine code.
This machine code is typically saved into an executable file. Once you have an executable file, you can run your program on any compatible computer system without needing the compiler or the source code.
In contrast, interpreted languages like Python need an interpreter program. This means that you need to have a Python interpreter installed to run Python code on your computer. Some may consider this characteristic a drawback because it can make your code distribution process much more difficult.
However, in Python, having an interpreter offers one significant advantage that comes in handy during your development and testing process. The Python interpreter allows for what’s known as an interactive REPL (Read-Eval-Print Loop), or shell, which reads a piece of code, evaluates it, and then prints the result to the console in a loop.
Note: In this tutorial, you’ll learn about the CPython standard REPL, which is available in all the installers of this Python distribution. If you don’t have CPython yet, then check out Python 3 Installation & Setup Guide for detailed instructions.
The Python interpreter can execute Python code in two modes:
- Script, or program
- Interactive, or REPL
In script mode, you use the interpreter to run a source file as an executable program. In this case, Python loads the file content and runs the code line by line, following the script or program’s execution flow. Alternatively, interactive mode is when you launch the interpreter and use it as a platform to run code that you type in directly.
Note: The name Python is commonly used to denote two different things: the language itself, and the interpreter. In this tutorial, you’ll find the explicit term Python interpreter only in situations where ambiguity can arise.
In this tutorial, you’ll learn how to use the Python standard REPL to run code interactively, which allows you to try ideas and test concepts when using and learning Python. Are you ready to take a closer look at the Python REPL? Keep reading!
What Is Python’s Interactive Shell or REPL?
When you run the Python interpreter in interactive mode, you open an interactive shell, also known as an interactive or a REPL session. In this shell, your keyboard is the input source, and your screen is the output destination.
Note: In this tutorial, you’ll find the terms interactive shell, interactive session, interpreter session, and REPL session used interchangeably.
The input consists of Python code, which the interpreter parses and evaluates. After that’s done, the interpreter automatically displays the result on your screen, and the process starts again as a loop.
So, Python’s REPL is an interactive way to talk to your computer using the Python language. It’s like live chat. The whole process is known as a REPL because it goes through four steps that run under the hood:
- Reading your input, which consists of Python code as expressions and statements
- Evaluating your Python code, which generates a result or causes side effects
- Printing any output so that you can check your code’s results and get immediate feedback
- Looping back to step one to continue the interaction
This feature of Python is a powerful tool that you’ll wind up needing in your Python coding adventure, especially when you’re learning the language or when you’re in the early stages of a development process. That’s because the REPL offers several benefits, which you’ll learn about next.
Why Use a Python REPL?
As a Python programmer, you’ll spend considerable time in interactive mode. This mode provides a quick way to try out ideas and code snippets. In a REPL session, you can do some or all of the following tasks:
- Explore and learn Python syntax
- Try out and prove ideas, concepts, and implementations
- Quickly evaluate code snippets
- Dive into the language behaviors
- Edit and refactor your code for later use in script mode
- Perform code and type introspection
- Get interactive help on how to use the language
- Run basic code debugging
- Explore standard-library and third-party modules, libraries, and APIs
- Inspect the implementation of classes, functions, and other objects
Clearly, as a Python developer, you’ll have many reasons to spend a lot of your time in REPL sessions, working interactively with the interpreter. Getting immediate feedback on how your code works is the most relevant benefit of using a REPL session.
Interactive mode is one of the best features of Python. It allows you to test solutions and experiment with the language in real time. If you want to know how something works, then just try it in an interactive shell.
You should definitely consider the interactive shell a powerful learning tool, especially if your previous programming experience has been with compiled languages that don’t provide a REPL.
While learning Python or exploring new features and concepts, you’ll note that many examples in the Python documentation, online tutorials, manuals, and courses are copied and pasted from an interactive session. You’ll recognize them because of the REPL’s characteristic prompts, which you’ll get to know in the Running the python Command section.
With this introduction to interpreters and REPLs under your belt, you’re ready to get into action. In the following session, you’ll learn how to start and end a Python interactive session.
Starting and Ending REPL Interactive Sessions
The Python standard REPL is available in every Python installation. To start a new REPL or interactive session, you just need to run the Python interpreter in interactive mode from your command line. This mode will put you into a shell environment where you can execute Python code. Learning how the input code works is basically a way of getting immediate feedback on it.
In the following sections, you’ll learn how to start a new Python interactive shell using the python command and some of its command-line options.
You’ll also learn about the standard look and feel of a Python interactive shell and about some of its core characteristics and features. Finally, you’ll learn how to terminate a Python interactive session.
Running the python Command
Once you’ve installed Python on your computer, then you can immediately start benefiting from this amazing tool. To start a Python interactive shell or session, open a command-line window and run the python command without arguments:
$ python
This command makes Python enter its interactive mode. The interpreter will run, and you’ll get an output that’ll look something like this:
Python 3.11.0 (main, Nov 6 2022, 13:27:29) ... on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
The first line of this output shows information about your current Python version and the platform on which you’re running the interpreter. The second line shows a message with commands that you can run to get additional information about Python in general.
The last line, which is highlighted in the output, shows the primary prompt of a standard Python interactive session or shell. By default, this prompt consists of three greater-than signs (>>>), also known as chevrons. Its purpose is to communicate that the interpreter is ready to accept input.
Note: The Python standard REPL doesn’t support syntax highlighting. However, in this tutorial, you’ll find that the examples are rendered using a syntax highlighter. Keep this detail in mind because you won’t get syntax highlighting when you run the examples on your computer.
The interpreter also has a secondary prompt represented by three dots (...). This prompt appears when you’re entering compound statements or line continuations. You’ll learn more about this prompt in the Running Compound Statements and Dealing With Explicit and Implicit Line Continuations sections.
Passing Command-Line Options to the python Command
The python command can take a bunch of command-line options. A few of them can be useful when working in a REPL session. One of the most relevant options in this context is the -i flag. This option makes the interpreter enter the interactive mode after running a script or executing a piece of code using the -c option.
Note: The -c command-line option allows you to quickly run a Python statement or expression that you provide as a string on your command line. Try out the command python -c "print('Hello, World!')" to see this option in action.
You can use the -i option to check the current global variables in your script or to inspect the stack trace when your program raises an exception.
To try this option out, say that you have the following sample script:
# sample.py
def read_data():
# Read data from a file or database...
return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sample = read_data()
def mean(data):
return sum(data) / len(data