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.
Read the full article at https://realpython.com/python-repl/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]