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
|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:
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:
The order of operation is to first do the multiplication, 2*3 =6. Then we do the addition which is now:
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:
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:
you should get an output of 6
Similarly, if you want to do subtraction, you could list:
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:
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:
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:
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:
You are not restricted to just using the number 1. You can increment X by increments of 2 by running:
or increments of 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:
The output will be 6.
If you want to divide 2 numbers, you can run:
The output should be 2.
Also just like in addition and subtraction, you can use variables:
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:
Each time you run X*=2, the value of X will double. For example:
After running each of the lines above, the Value of X will now be 6. You can verify that by running:
You can also do this with the division operator:
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:
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:
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:
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:
Or you can write it out as:
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:
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.
The oposite of exponents is a root. As we stated earlier,
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:
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:
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:
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:
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.