Mathematical Operations in Python | That you need to know

Mathematical Operations in Python | That you need to know

Karan's photo
·Dec 8, 2022·

4 min read

I'd like to show you some of the other mathematical operators that you can use in addition to adding. Starting with adding


Subtraction is the next obvious one.


You'd just use the minus sign, but when it comes to multiplication, things get a little strange. You actually use the Asterix instead of the x or some sort of time symbol. So you'd say something like this:

4 * 2
5 * 2

And that's 4 times 2 and 5 times 2. The final one is division, which is done with the forward slash


When you print that, you'll get 2. One thing to keep in mind here is that whenever you divide something, you always end up with a floating point number. As you can see, even though six divides cleanly into three, we will get 2.0. In fact, if I wrap this division in a type check, it will output a float rather than an integer.


And division in Python simply causes this to happen. It doesn't really matter if the result is a float or an integer because you still receive what you need in the end.

2 ** 2

Two asterisk signs, which give you access to the exponents or when you want to increase the number to a power, are the final one that is incredibly helpful. similarly, and 2 to the power of which will equal 4. One of the reasons that a lot of data scientists and mathematicians really adore Python is because it is so well-suited for handling and manipulating numbers thanks to the exponent being built into the language. When performing these mathematical operations, one thing to keep in mind is that there is a certain level of priority when there are multiple operations on the same line of code. Therefore, some of these operations, such as multiplication and division, will be of first-class, While others, like the plus and minus, will be more cost-effective. The rule from high school that you might recall is something called



In essence, it says that parenthesis, exponents, multiplication, division, addition, and subtraction are in that sequence of importance. Therefore, the items inside of the brackets come first, followed by our exponents, multiplication, and division. Finally, our addition and subtraction have the lowest importance. It is somewhat misleading due to this sequence. It gives the impression that multiplication occurs before division, but they are both as significant. In fact, when it comes to your computations, the operation that is most to the left will be given precedence over multiplication and division. To better understand this, let's look at an actual case from real life. Consider the case where we wished to multiply in a line of code.

print(3 * 3 + 3 / 3 - 3)

This entire line of code will be calculated for you if we run it in its entirety and print it to the console. What will be printed exactly is a number. But the hierarchy counts. Does it first add 3 to 3 before multiplying the result by 3 or does it first divide by 3 before adding 3 to it? What is the hierarchy?

Using the rule, PEMDAS,

It doesn't matter because we can see that what is in the parenthesis is what happens first. Our exponents are the following item, but there isn't one present. We don't have anything to the power of anything or 2 to the power of 3. So, we can also disregard that. Division and multiplication are therefore the next stage. As I just said, they are all equally important. Therefore, the calculations 3 * 3 and 3 / 3 are both equally significant, but they are done left to right. Thus, multiplication is actually what we observe first. So if it helps, you might want to finish this mnemonic with LR. Thus, even though this was a division and this was a multiplication, this calculation will always be carried out first, becoming PEMDASLR. What do you believe this number would be, going back to the original question I posed to you? When we execute this line of code, we get the value 7.0. These are the math operations available in Python.

Thank you for reading!

Buy Me a Coffee at

Check out my profiles on these platforms.

Did you find this article valuable?

Support Karan by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
Share this