To learn more about the functions covered in this lesson, check out:
First up is
abs(), the absolute value of a number. Here,
abs() will have the
5 placed in there.
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.
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.
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.
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.
We can enter two numbers, seen here
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.
divmod(10, 3), we get
3 3s go into there and we have
1 as our remainder. Next up,
max(), as seen onscreen, gives us the maximum of an iterable, so if we pass it a list of numbers—in this case,
5—we’ll get the maximum, which is
An important thing to remember here is this isn’t the maximum absolute value, but it’s maximum value. So
2—the maximum number that it gives back is
-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
5, we predictably get the output
And passing it
2, as we would expect, we get the answer
-4. Next up,
pow() can take either two or three arguments.
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
9, which is exactly the same as typing
3, double star (
**) for the power operator, and
pow() can also take that third modulo argument, as we’ll see here.
And there you can see the remainder from that, when divided by
1. That’s equivalent to what’s seen onscreen now.
The next function is
round(). And as you’d expect,
round() rounds a number to the nearest integer. In this case,
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
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.
Next up, you can see
sum() sums up the iterable that you pass to it with an optional
start value. Here it’s being passed the numbers
5, and it sums them to
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
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.
@stennow: Find out why in our dedicated tutorial on rounding behavior in Python: realpython.com/python-rounding/ :-)
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…