The Python REPL

Exploring The Python REPL

OK — let’s go! We’ll start inside the Python REPL. With your terminal open and the Python interactive shell started, you’ll see a command prompt consisting of three arrows (>>>). If you use, the command prompt looks a bit different.

Just to be absolutely clear, you don’t type in the three arrows, only what follows after it. Now type in the number 10:

>>> 10

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: and it’s ready for the next input

Let’s give it something more challenging:

>>> 10 + 10

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

Basic 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 operators you can use. Go ahead a play around with this in the REPL:

+Addition2 + 2 == 4
Subtraction3 – 1 == 2
*Multiplication5 * 3 == 15
/Division5 / 2 == 2.5
The basic operators most of you will know

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

%Modulus5 % 2 == 1
//Floor division9 // 2 == 4
**Exponential2 ** 4 == 16
Some more advanced operators

Operator precedence

Operator precedence, the order in which Python processes the operators and numbers, is the same as in math. For example, multiplication and division come before addition and subtraction. In case of doubt, you can always use parentheses, or simply try it in the REPL.

Let’s try some examples:

>>> 2 + 3 * 3
>>> (2 + 3) * 3
>>> 1 + 2 ** 2
>>> 2 / 2 * 8

Please help us and share this article

About the author

Erik is the owner of Python Land and the author of many of the articles and tutorials on this website. He's been working as a professional software developer for 25 years, and he holds a Master of Science degree in computer science. His favorite language of choice: Python!