Getting Started With min() and max()

00:00 Getting Started With Python’s min() and max() Functions. Python includes several built-in functions that make your life more pleasant and productive. They mean you don’t need to reinvent the wheel.

00:12 Two such functions are min() and max(). They mostly apply to iterables, but you can use them with multiple regular arguments as well. As their names suggest, they take care of finding the smallest and largest values in input data.

00:27 Whether you’re using Python’s min() or max(), you can use the function to achieve two slightly different behaviors. The standard behavior for each is to return the minimum or maximum value through straightforward comparison of the input data as it stands.

00:41 The alternative behavior is to use a single-argument function to modify the comparison criteria before finding the smallest and largest values. To explore the standard behavior of min() and max(), you can start by calling each function with either a single iterable as an argument or with two or more regular arguments, and that’s what you’ll do straight away.

01:02 The built-in min() and max() have two different signatures that allow you to call them either with an iterable as their first argument or with two or more regular arguments.

01:11 The signature that accepts a single iterable argument looks as seen on-screen. Both functions take a required argument called iterable and return the minimum and maximum values, respectively.

01:22 They also take two optional keyword-only arguments: default and key. On-screen is a summary of what the arguments to min() and max() do. In these signatures, the asterisk (*) means that the following arguments are keyword-only arguments, while the square brackets denote that the enclosed content is optional.

01:43 Later in this course, you’ll learn more about the optional default and key arguments. For now, just focus on the iterable argument, which is a required argument that leverages the standard behavior of min() and max() in Python.

01:58 The first call to min() returns the smallest number in the input list—in this case, -5. If you pass an empty iterator to min() or max(), then you get a ValueError because there’s nothing to do on an empty iterable.

02:12 The first call to max() returns the largest number in the list, 9. An

02:19 important detail to note about min() and max() is that all the values in the input iterable must be comparable. Otherwise, you get an error.

02:28 As you can see on-screen, numeric values work fine.

02:36 These examples combine int and float numbers in the calls to min() and max(). You get the expected result in both cases because these data types are comparable.

02:47 But what would happen if you mix strings and numbers? As ever, the best way to find out is to give it a try.

02:58 You can’t call min() or max() with an iterable of noncomparable types as an argument. In this example, a function tries to compare a number to a string, which is like comparing apples and ladders.

03:10 The end result is that you get a TypeError.

03:16 The second signature of min() and max() allows you to call them with any number of arguments, providing that you use at least two.

03:23 This signature has the form seen on-screen. Again, these functions return the minimum and maximum values, respectively. The meaning of the arguments in these signatures is shown on-screen.

03:36 This variation of min() or max() doesn’t have a default argument. You must provide at least two arguments in the call for the function to work correctly.

03:44 So, a default value isn’t required because you’ll always have at least two values to compare in order to find the minimum or maximum. You can see this alternative signature in use on-screen. You

04:00 can call min() or max() with two or more regular arguments. As you’d expect, you get the minimum or maximum value in the input data.

04:09 The only condition is that the arguments must be comparable.

04:15 In the next section of the course, you’ll look at using min() and max() with strings and iterables of strings.

Become a Member to join the conversation.