Functions: Math
To learn more about the functions covered in this lesson, check out:
00:05
First up is abs()
, the absolute value of a number. Here, abs()
will have the int
5
placed in there.
00:18
And we can see it returns the absolute value. If we do abs(-5)
, it also returns 5
. So effectively, it removes the sign from the number that you pass in.
00:30
Next up, the divmod()
function. And before we get to that, a quick recap of the modulo function, which gives you the remainder when a division is performed. It uses the percent sign (%
), and will give you one number back which is the remainder from the division that you’ve asked for.
00:50
In this example, we’re going to take the number 15
, then the modulo operator with the %
and then 4
, and the answer is 3
because that’s the remainder when you divide 15 by 4.
01:05
But there are situations when you want both the amount that the number divides into and the remainder as well, and that’s where divmod()
comes in.
01:14
We can enter two numbers, seen here 15
and 4
, and we get 3
because 4 goes into 15 3 times, and we get a remainder of 3
. Another example will give us some different numbers to show which is which.
01:30
So divmod(10, 3)
, we get 3
3s go into there and we have 1
as our remainder. Next up, max()
. max()
, as seen onscreen, gives us the maximum of an iterable, so if we pass it a list of numbers—in this case, 1
to 5
—we’ll get the maximum, which is 5
.
01:52
An important thing to remember here is this isn’t the maximum absolute value, but it’s maximum value. So -4
and 2
—the maximum number that it gives back is -2
, as -2
is greater than -4
. Now of course, we need the opposite function to that, and that is min()
, which will do a similar job but give us the minimum from any iterable that we pass to it. So here again, passing that same list of numbers 1
to 5
, we predictably get the output 1
.
02:27
And passing it -4
and 2
, as we would expect, we get the answer -4
. Next up, pow()
. pow()
can take either two or three arguments.
02:42
With two arguments it’s equivalent to x
to the power of y
, but with a third argument it performs a modulo operator on the result. Here we can see 3
to the power of 2
is 9
, which is exactly the same as typing 3
, double star (**
) for the power operator, and 2
.
03:00
But pow()
can also take that third modulo argument, as we’ll see here.
03:09
And there you can see the remainder from that, when divided by 2
, is 1
. That’s equivalent to what’s seen onscreen now.
03:19
The next function is round()
. And as you’d expect, round()
rounds a number to the nearest integer. In this case, round(4.5)
is 4
and round(4.6)
is 5
.
03:33
Now, this may appear similar to the int()
function you saw earlier on, but we can see that actually it’s different. int(4.5)
gives us the same answer but int(4.6)
also gives us 4
.
03:47
The reason for this is int()
just ignores what happens after the decimal place and takes just the integer part, whereas round()
takes it into account.
03:56
Next up, you can see sum()
, and sum()
sums up the iterable that you pass to it with an optional start
value. Here it’s being passed the numbers 1
, 2
, 3
, 4
, and 5
, and it sums them to 15
.
04:12
But you can also add in an optional start
value, which can be useful in some situations. By performing the same calculation but with a start
value of 10
, you can see that it comes to the sum of 25
.
04:30 This is the end of the math section, and while none of these functions are particularly exciting, it’s important to remember that they’re there as they will save you time in having to code your own solutions, which could possibly have bugs in and would probably be slower than these equivalents.
Dan Bader RP Team on April 3, 2020
@stennow: Find out why in our dedicated tutorial on rounding behavior in Python: realpython.com/python-rounding/ :-)
Uğur on Jan. 13, 2021
Very helpful. Thanks
Become a Member to join the conversation.
stennow on April 3, 2020
why does the round(4.5) not round up to 5? Why would a math funktion not do math…