Overview of Popular Math Functions in Python
So far in this section on Python numbers we've walked through how we can work with the various number types in python so how we can perform computations how we can view the data types. The order of operations and different elements like that. And that is all very important foundational knowledge.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Now with all of that in place now we want to show you a glimpse into the full set of functions that are available to the number data type inside a python. And so what we have here is going to be a look at how we can automate a number of the processes that we've talked about and how we can have helpful processes called on numbers. And so the very first thing I'm going to do at the very top is I'm going to import the math library now Python is famous for working very well on mathematical calculations and part of it is due to having a very big math library where you can have all these great functions and we're going to walk through some of the most popular ones that are out there.

I've created two variables. One is a negative called loss and it's -20.25. So this is a floating-point number that's negative and the next one's a floating-point number called product_cost at 89.99. And we're going to walk through a number of various functions that you may find helpful when working with numbers in python. So the first one I'm going to put all these in print statements is abs which stands for the absolute value of.

So you say ABS loss and what it's going to do is it's going to take the absolute value of loss and if your math is a little bit rusty then absolute value that's going to do is it's going to take just the pure value and it's going to remove this negative sign right here. So now if I run this you can see the value is 20.25

large

So that is simply available to the number class inside a python and if you want to test it out if you delete or just remove or comment out. What we had there on line 1 you'll see that that still works. So the math library is not needed for absolute value, that is in the core Python library.

Now let's go with one that does need it so I'm going to say right here math and that's because we're importing the math library. So I'm going to say math.floor and then call in product cost. So now let me run this and you can see that the value is 89. And so what we have here at the product cost is it looked at it, returned an integer, and it picked the floor of this product cost float. Technically you also could just call int on this as we've covered before. So if I just call int on product cost. This gives us the same exact value.

Now one reason why you may not want to do that is because math.floor very clearly shows your intention. When it comes to programming one of the most important elements is not just making sure that your program works properly but it's also being very explicit on your intentions as you're coding. So if you want the lower bottoms if you want the floor value of this product cost variable here. When you say math floor when you're reading this later on or if someone else is reading it they're going to understand that you wanted the lower value the rounded down lower value of product cost. If you just use int even though the value would have been the same your intention isn't as clear because when I see the word int when I see that function call. My first thought is that whoever the developer was even if it was just me is that the intention was to convert the value to an int.

Now it has a side effect of also giving you the floor value of that int because it takes away any floating-point numbers. However, my intention with that wouldn't be clear. Where right here it's very explicit. I want the rounded down lower version of that and the other issue with it is we can do the opposite. So math also has a contrapositive called ceil which is short for ceiling and so now if I run this you'll see that we get 90.

large

Ceiling is going to give us the rounded up value. So if I say 89.12 you can see it still returns 90 it always is going to round up to the next whole number and it's going to return an integer as you can see right here.

large

Now the other thing you can do that's pretty cool is you can combine these. So say that I'd come here and I want to get the absolute value of loss but I also want the floor. So you saw the absolute value of loss returns 20.25 But I can say math floor and wrap that and parens and now if I run this I get 20.25 rounded in this case down which technically looks like it's wrong right? because it's 21. This looks like the ceiling value.

However, what you have to realize is that math floor is being called before the absolute value. So let's do this let's take it in a couple of steps here. So if I come here and I remove the absolute value call and run at it gives us negative 21 because the first value is -20.25 So we're actually going in the opposite direction. And just so that you believe me. Let's do it even without that let's just do loss by itself. So loss by itself is going to return -20.25

large

So when we call floor on it it actually is going to give the opposite as if it were a positive number so -21 is the floor value because it is lower for a negative number. And the whole point on why I wanted you to see that was so that you could see the way the priority works. So if I take it back to having the absolute value in there. The way it works is math floor on loss is what would be called first then the absolute value would be called on that. That's the reason why if you run that you get 21 that's something that's important to know whenever you are nesting functions like that.

Moving down let's go with another one so far we've covered absolute value for ceiling and then we've seen how we can combine those and the next one we're going to do is round. So this is when you simply want to round to the nearest whole number so I can say round here and then let's pass in the product cost. So right now it's at 89.99 If I run this it's 90.

large

Now if I change this back to 89.12 it's 89.

large

So if you are curious about how you can get to the closest whole number then round is a nice way to do that. So we only have two more that we're going to cover. They are square root and then we're going to work with exponents. So I'm going to copy that. And now instead of round I'm going to say math.sqrt and that's how you spell square root in the function. S Q R T and now if I run this we get a floating-point number of 9.48 and then a string of values

large

so that is how you can quickly get the square root which is an incredibly helpful little function that I've used a number of times.

Now the very last one we're going to do is called pow. And so I can say math.pow and then it takes two arguments. And so if I do five comma two and print this out it's 25 because it's going to be five squared.

print(math.pow(5, 2))   #25.0

Now, this is the equivalent as you may have noticed to simply doing this because we have our exponent operator right here. And if I run that I get the exact same number.

large

If you're curious about why you'd want to use one versus the other. The answer is in the output. Do you notice how we get a floating-point number when we use math.pow and when we simply use the operator the exponent operator we get an integer back. And so what that means is if you go through the math library and you look at the pow function you're going to see that it is much more complex and much more scientifically accurate. So if you're using exponents in large scientific calculations then you're going to want to use math POW versus if you just want a simple exponent you can use the basic exponent operator just like we have right here.

So review those are some very common and popular functions that include are included in the math library and in the standard number class in Python.

Code

import math

loss = -20.25
product_cost = 89.99

print(abs(loss))
print(math.floor(product_cost))
print(math.ceil(product_cost))
print(abs(math.floor(loss)))
print(round(product_cost))
print(math.sqrt(product_cost))
print(math.pow(5, 2))
print(5 ** 2)