What is REPL? REPL is the language shell, the Python Interactive Shell. The REPL acronym is short for Read, Eval, Print and Loop.
The Python interactive interpreter can be used to easily check Python commands. To start the Python interpreter, type the command python without any parameter and hit the “return” key.
The process is:
- Read: take user input.
- Eval: evaluate the input.
- Print: shows the output to the user.
- Loop: repeat.
Related Course: Complete Python Programming Course & Exercises
Introduction
You can check the version of Python you have installed by typing python without pressing enter, instead pressing the tab key. This will return the Python versions installed on your computer
➜ ~ python |
To get more details on the version, you can type the command:
python --version |
You should be using Python 3 or newer (2 is legacy).
REPL
REPL is the Interactive shell
To start the Python language shell (the interactive shell), first open a terminal or command prompt. Then type the command python and press enter.
Python then outputs some information like this (including the Python version):
$ python |
After the symbols >>> you can type a line of Python code, or a command.
Each command at the prompt is evaluated by Python. If you would type, if it’s incorrect it returns an error.
hello world |
In the above example it’s incorrect because text must be between quotes.
"hello world" |
On some versions of Ubuntu you need type python3 instead of python.
You can type all kinds of commands in the interactive shell. If you want to use it as calculator, just type your calculation:
128 / 8 |
Variables can be used in the Python shell too:
width = 10 |
Note: We’ll teach you how to start python programs in the next article.
How to Quit the Python Shell
If you started the Python interactive shell, you may be wondering how to exit it. There are several ways to quit the Python interactive shell (REPL).
One way is by typing exit(). This is calling the function exit, which is why the brackets in exit() must be written.
exit() |
A keyboard shotcut can be used to exit the shell too: Ctrl-D.
If you are a Python beginner, then I highly recommend this book.
