Python Math Operators

Understanding the Python Math Operators is very important. You use these operators throughout the Python language whether it be concatenating strings, doing basic algebra, complex calculus, or delving into data science.

In this article, we will cover the basics of doing math with python including how to do multiplication, division, addition substraction, exponents, and orders of operation.

What is an Operator?

A math operator is a symbol that defines a specific kind of logic. For example, if you a plus sign ‘+’ that means addition. So you add the two numbers together. Below I have create a table with some of the most common math operators that you will be using in Python

DescriptionOperatorExample
Add+1+2=3
Subtract-3-2=1
Multiply*2*3=6
Divide/6/3=2
Floor Division//5//2=2
Remainer%5%2=1
Exponent**2**3= 8
Root or Square Root**(1/x)81**(1/4.0) = 3.0

In the upcoming sections we will discuss these operators in more detail.

Order of Operation

The order of operation is the order in which you evaluate different math operators. This is important because the order which you do things can have a big effect on the outcome of your equation. For example, if you have the following equation:
1+2*3

If you add 1 and 2 together, then multiply by 3, your answer would be 9. However, if you multiply 2 and 3 together, then add 1, your answer is 7. There is an acronym about orders of operation that is good to remember: PEMDAS.
This stands for Parentheses, Exponents, Multiplication, Divisian Addition, Subtraction. If you can remember this acronym, then you can remember the correct oder of operations. This phrase means that you start by evaluating anything in parentheses, then finish with addtion and subtraction.

If we look back at our sample formula:
1+2*3.
The order of operation is to first do the multiplication, 2*3 =6. Then we do the addition which is now:
1+6=7

However, if we introduce Parenthesess, we can change the formula to:
(1+2) * 3
This means we first do what is in the parentheses, which is 1+2=3. We then do the multiplicaiton part, which is now:
3*3=9.

Python should always do the order of operations correctly. However, it is important for you to understand how it is working. Otherwise you might have some unfortunate surprises when you are writing your equations. If you are ever un-sure about the correct order of operations, you can always use parentheses to force a specific order.

Addition and Subtraction

Addition and Subtraction are these most common operators you will sue in math. So it is good that it is also the simplest section.
If you want to add two numbers together, you just list for first number a ‘+’ sign, then your second number. For example:
2+4
you should get an output of 6
Similarly, if you want to do subtraction, you could list:
6-4
The output should be 2.

These basic operations are not that helpful when you are manually keying in the numbers. Ultimately you want to use variables for performing these operations. Here is another example:
X=2
Y=4
X+Y

In the above example, we have set the variable X to a value of 2, then the value of Y with a value of 4. We then asked python to add the two together with X+Y. The output to this should be 6. Just like when we manually typed in the numbers, pyhon is substituting the correct values based on what we set the variables to.

The same thing works when we do subtraction:
X=2
Y=4
Y-X
The output to the above equation will be 2 because 4 – 2 = 2

Things get even more interesting when you start combining operations. An exmple of that would be:
X+=1

The above operator is telling X to increment by 1. Another way to write this same operation is:
X=X+1. We are taking X, adding one to it, then setting that as the new value of X.

We can do the same thing with subtraction to decrement the value of X by 1:
X-=1

You are not restricted to just using the number 1. You can increment X by increments of 2 by running:
X+=2

or increments of 10:
X+=10

Multiplication and Division

Now that you understand how addition and subtraction work, multiplication and division should be a lot easier to pick up. The logic behind how to do the operations in python is exactly the same. You just need to know the right operators.

If you want to multiply two numbers, you can simply write:
2*3
The output will be 6.

If you want to divide 2 numbers, you can run:
6/3
The output should be 2.

Also just like in addition and subtraction, you can use variables:

X=2
Y=4
X*Y
The output from above will be 8

Or if you do Y/X the output will be 2.

Also just ike in the previous section, you can combine operators to increment and decrement the value of a variable:
X=1
X*=2

Each time you run X*=2, the value of X will double. For example:
X=1
X*=2
X*=2
X*=2
After running each of the lines above, the Value of X will now be 6. You can verify that by running:
Print(X)

You can also do this with the division operator:
X/=2
Each time you run the above, the value of X will be cut in half.

Floor Division and Remainders

In the previous section we talked about regular division. But we also were concentrating on whole numbers. If you use regular division and you take two numbers that don’t divide to a whole number, you then have to deal with decimals. For example:
2/5=2.5

There are times when you don’t want to deal with the decimals. You just want to focus on the whole numbers and ignore the decimals. There are other times that you don’t care all that much about the result. But you want to know how much is left over after the decimal. These two cases are when you would use Floor Division (A.K.A. Modulus), and remainders.

The floor division operator is a double slash “//“. If we use this operator in our original equation, it will look like this:
5//2=2

As you can see, the result is the same as the original equation, but we ignored the values following the decimal point. You might use this if you have 5 apples and you want to divide the into two piles, how man apples should you put in each pile? This equation would give you the answer. 2 apples in each pile. You can’t go any higher without cutting the extra apple in half.

Another operator that is related to floor division is calculating the remainder. The remainder is the value left over after you do division. For example:
5%2=1

This equasion is saying after we divide 5 by 2, we have one left over. To re-use the apple analogy, if we have a pile of 5 apples, and we want to divide it into two equal piles, we will end up with two piles of apples with 2 apples each. But we will have one left over because to put it into either pile would make that pile no longer equal.

The above eqution shows that if you divide 4 by 2 you can do it without anything left over, so 4 is divisible by 2.

Exponents and roots

Exponents are when you take a number and multiply it by itself multiple times. And roots are when you do the same thing, but wit h division.

For example, 2 to the third power would be written as 2**3 in python. and the answer is 6.
The long way to write this out would be:
2*2*2=6

Or you can write it out as:
2**3

That is a lot shorter. In this case where we are only doing 2 to the third power, you could realistically write it out and it is not that big a deal. But what if you need to write out:
2**87

Yo don’t want to write out 2*2*2*2……etc… 87 times.

Just like in the other secitons, this is more valuable once you start using variables.
X=2
Y=3
X**y
Output: 6

The oposite of exponents is a root. As we stated earlier,
2**3=6
or
2**2=4

This means that 2 squared, or 2 times itself equals 4.

When we are calculating square roots, we are asking ourselves what number times itself, or what number to the second power equals the number in question. We can calculate a square root with the following:
4**(1/2.0)

The output to the above formula is 2, which is correct. Notice I put (1/2.0) and not (1/2). If you only put 1/2 then Python will calculate the answer as 1 instead of 2. This is because of a quirk in the way that Python evaluates the exponent.

Another way to do square roots is by importing the python math library, and then using the sort() function. An example of that is:
import math
X=4
math.Sqrt(X)

This small script will rightly output the square root of 4 as 2.0. The limitation to using this function is it only works for square roots. Not Cube roots or higher. As an exampe:
3**4=81
What if you want to know what number to the fourth power equals 81? You can’t easily do it with the math library, so we use our original formula. We would write:
81**(1/4.0)
Output: 3.0

Summary

At this stage you should now know how to do the basic math operators in Python. I would encourag you to spend some time playing around with the different operators and making sure you understand how they work. Think of some real world examples of when you could use these math operators.

%d bloggers like this: