The Python REPL

We’ll start our Python learning journey in something called 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

With your terminal open and the Python interactive shell started, you’ll see a command prompt consisting of three arrows (>>>). 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
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
20

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.

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 of the operators are the same in other programming languages, so it pays to learn them well.

Go ahead a 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
%Modulus5 % 2
//Floor division9 // 2
**Exponential2 ** 4
Some more advanced operators

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. However, in Python multiplication and division have the same precedence, so the order matters too. 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.0

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.

You can obtain the result of the last expression in a Python REPL with the underscore operator, e.g. in the Python REPL this looks like:

>>> 3 * 3
9
>>> _ + 3
12

Using the history

Have you noticed that Python keeps a history of commands too? 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.

Storing results

Terrific, we can already do some math in Python, and we can 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.

Please share this article with others!

Python Courses

Are you enjoying this free tutorial? Please also have a look at my premium courses. They offer a superior user experience with small, easy-to-digest lessons and topics, progress tracking, quizzes to test your knowledge, and practice sessions. Each course will earn you a downloadable course certificate.

The Python Fundamentals Course For Beginners
Now for $49 (from $59)

Python Fundamentals I is a course for beginners that will get you started with Python in no time. Learn all the essentials, test your progress with quizzes and assignments, and bring it all together with the final course project!

Python Course for Beginners

Modules, Packages, And Virtual Environments
Now for $49 (from $59)

Python Fundamentals II covers creating your own modules and packages, using virtual environments and Python package managers to make your life as a programmer easier. Advance your productivity as a Python programmer!

Python Fundamentals 2

Leave a Comment

Share to...