Coding Custom min() and max() Functions
00:00
With the min_max()
helper function in place, you can define your custom versions of min()
and max()
. Go ahead and add the code seen on-screen to the min_max.py
file.
00:14
First, you import gt()
and lt()
from the operator
module. These functions are the functional equivalent of the >
and <
respectively. For example, the expression seen on-screen is equivalent to the function call seen on-screen.
00:30
You’ll use these functions to provide the operator
argument to min_max()
. Just like min()
and max()
, custom_min()
and custom_max()
take *args
, key
, and default
as arguments and return the minimum and maximum values, respectively. To perform the computation, these functions call min_max()
with the required arguments and with the appropriate comparison operator
function. In custom_min()
, you use lt()
to find the smallest value in the input data. In
01:13
custom_max()
, you use gt()
to get the largest value. You’ve finished coding your own versions of min()
and max()
in Python.
01:31 Now go ahead and give them a try. You can see them in action on-screen.
02:55 In the next section of the course, you’ll take a look back at what you’ve learned.
Become a Member to join the conversation.