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.
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
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
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.
Become a Member to join the conversation.