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.
Table of Contents
Exploring The Python REPL
With your terminal open and the Python interactive shell started 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.
Now type in the number 10:
>>> 10
10
Code 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
20
Code 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:
Operator | Name | Example |
---|---|---|
+ | Addition | 2 + 2 |
β | Subtraction | 3 β 1 |
* | Multiplication | 5 * 3 |
/ | Division | 5 / 2 |
If you know your math, you might also want to try:
Operator | Name | Example |
---|---|---|
% | Modulo | 5 % 2 |
// | Floor division | 9 // 2 |
** | Exponential | 2 ** 4 |
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.0
Code 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.
You can use the result of the prior expression in a Python REPL with the underscore operator, it works like this:
>>> 3 * 3
9
>>> _ + 3
12
Code language: Python (python)
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.
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.