The Python REPL

We’ll start our Python learning journey with the Python REPL. It’s an interactive shell that allows you to enter Python commands and directly see the results. It’s a great way to tinker and learn! We’ll use the REPL as a calculator and explore Python’s operators.

Exploring the Python REPL

To start the REPL, open a terminal and type the command that starts Python:

  • On macOS and Linux: python3
  • On Windows: python, or py if you installed Python with the Python install manager

If that doesn’t work, or you want more detail, read how to start Python first. When the REPL is ready, you’ll see a command prompt consisting of three arrows (>>>). To be clear, you don’t type in the three arrows; you only type what follows after it.

When you’re done, type exit and press Enter to leave the REPL. You can also press Ctrl+D on macOS and Linux, or Ctrl+Z followed by Enter on Windows. On Python versions older than 3.13, you have to type exit() with the parentheses.

Now type in the number 10:

>>> 10
10Code language: Python (python)

What happened? Remember we are in a REPL, which is short for Read-Evaluate-Print-Loop:

  • Read: Python reads 10
  • Evaluate: Python evaluates this input and decides it is a number
  • Print: it prints out what was evaluated
  • Loop: it’s ready for the following input

Let’s give it something more challenging:

>>> 10 + 10
20Code language: Python (python)

This time, Python sees our two numbers and a so-called operator, the plus sign, and evaluates this to 20. Yup, the Python REPL can be used as a calculator.

Arithmetic operators

OK, so Python is great at doing math. In fact, it can replace your calculator easily. A little confession: I use the Python REPL as a calculator all the time!

We’ve seen how to use the + operator. It’s just like regular math. Let’s go over some of the other arithmetic operators you can use. Some will look familiar; others might look a bit odd. You’ll get used to it quickly, and most operators are the same in other programming languages, so it pays to learn them well.

Go ahead and play around with this in the REPL:

OperatorNameExample
+Addition2 + 2
–Subtraction3 – 1
*Multiplication5 * 3
/Division5 / 2
The basic operators most of you will know

If you know your math, you might also want to try:

OperatorNameExample
%Modulo5 % 2
//Floor division9 // 2
**Exponentiation2 ** 4
Some more advanced operators

Want to try these arithmetic operators yourself? Practise calculations with Python in a free browser exercise. Write the calculations, run your code, and check the results.

Operator precedence

Operator precedence, the order in which Python processes the operators and numbers, is somewhat similar to regular math. For example, multiplication and division come before addition and subtraction. Multiplication and division have the same precedence, so the order matters. Like in math, we work from left to right. E.g., 2 * 2 / 8 = 0.5 , while 2 / 2 * 8 = 8.0.

If in doubt, you can always use parentheses. Alternatively, you can try it in the REPL and see what happens.

Let’s try some examples:

>>> 2 + 3 * 3
11
>>> (2 + 3) * 3
15
>>> 1 + 2 ** 2
5
>>> 2 / 2 * 8
8.0Code language: Python (python)

Using the underscore to get the previous result

Now that we’re getting more and more advanced, here’s a little trick I’d like to show you that can save you time.

In the REPL, Python stores the result of the last expression in a special variable called _ (the underscore). You can use it in your next calculation like this:

>>> 3 * 3
9
>>> _ + 3
12Code language: Python (python)

This only works in the REPL, not in a Python program you run from a file. There’s more to the underscore, which you can read about in the underscore in the Python REPL.

Using the history

Have you noticed that Python also keeps a history of commands? You can go back and forth between previous commands by pressing the up and down arrows. Python keeps this history on a file (on most OSes in ~/.python_history), so it even persists between sessions.

The REPL in Python 3.13 and newer

Python 3.13 came with a new, friendlier REPL, and 3.14 improved it further. If you use a recent Python, you’ll notice these differences from older tutorials and screenshots:

  • Prompts, error messages (tracebacks) and, since 3.14, your code itself are shown in color.
  • You can type exit, quit and help without parentheses.
  • After a line that ends with a colon, like if or def, the REPL indents the next line for you.
  • Multi-line code is edited and recalled as a whole block. Press the up arrow once and you get your entire for loop back, ready to edit, instead of one line at a time.
  • F1 opens the interactive help, F2 shows your history without the prompts and output, and F3 turns on paste mode, so you can paste larger blocks of code without the indentation getting mangled.

If the new REPL causes problems, for example in an unusual terminal, you can get the old one back by setting the environment variable PYTHON_BASIC_REPL to any value before starting Python. The Python documentation on interactive mode has the details.

Storing results

Terrific! We can already do some math in Python and even use previous results. But it would be even more awesome if we could store the results of our calculations. For that, Python allows us to define variables, which is the next topic of this tutorial.

Related articles