Operations#

Amanda R. Kube Jotte

To work with data in Python, we need to know the basic mathematical operators. In this section, we will see how to use Python to perform mathematical operations such as addition, subtraction, multiplication, division, and exponentiation.

Addition and Subtraction#

Addition and subtraction are relatively self explanatory in Python. We use the operators + and - as we would on a calculator. For example, we can type \(3 + 4\) in a Jupyter notebook cell and run it to output the solution, 7, as follows:

3 + 4
7

We can do the same for subtraction, subtracting 6 from 10 to get 4.

10 - 6
4

We can create longer expressions as well.

2 + 5 - 4 + 10 + 2
15

Multiplication and Division#

The operator for multiplication in Python is *, which we can use as follows.

3 * 6
18

There are multiple types of division in Python. We can use / for normal division which may result in a number with a decimal. For example:

7 / 2
3.5

We can also do floor division using //. Floor division is equivalent to dividing and then taking the floor of the result (rounding down to the nearest whole number).

7 // 2
3

We can also use the modulo operator % to get the remainder after division. Using the same example as above. We know that two goes into 7 three times leaving a remainder of 1. Therefore, 7 modulo 2 gives the result 1.

7 % 2
1

Let’s try another example.

13 / 5
2.6

Thirteen divided by five is 2.6. The floor division of Thirteen and five, is 2 as five goes into 13 two times.

13 // 5
2

And 13 modulo 5 is 3 as 3 is the remainder.

13 % 5
3

Exponentiation#

We can also compute exponents using Python. The operator for this is **. This is an example of a time where syntax, or the arrangement of letters and symbols, is important. Adding a space as in * * will not work!

2 ** 3
8
2 * * 3
  File "<ipython-input-64-0f343c0fc354>", line 1
    2 * * 3
        ^
SyntaxError: invalid syntax

The expression 2 * * 3 produces an error as Python does not recognize that you meant **.

To take the root of a number in Python, we simply exponentiate by the reciprocal. For example, to take the square root of 4 or the cubed root of 8, we would do the following.

4 ** (1/2)
2.0
8 ** (1/3)
2.0

Order of Operations#

Notice that we had to use parentheses above. If we would not have included those parentheses, we would have gotten a different result. For example:

8 ** 1/3
2.6666666666666665

Python follows the same order of operations that you learned in elementary school. You might remember learning a pneumatic phrase such as ‘Please Excuse My Dear Aunt Sally’ to remember in what order to execute operations: Parentheses, Exponentiation, Multiplication and Division, Addition and Subtraction. In the previous code cell, Python computed exponentiation first, computing 8 to the first power, then divided by 3. Using parentheses not only avoids this issue, but makes your code easier to read as well! For example, these give the same result, but the second is much more clear.

5 + 10 ** 2 - 5 // 2 * 4
97
5 + (10 ** 2) - ((5 // 2) * 4)
97

In the next section, we will see how to save these results by assigning them a name and how we can use these names in further computation.