Using Functions: Arguments and Return Values
max() and min() are built-in functions that find the largest and smallest values in any collection.
One Value from Many
A collection can contain several values, but sometimes you need only its largest or smallest value. Python provides two built-in functions for this purpose: max() finds the largest value in a collection, and min() finds the smallest value. The collection is supplied as an argument, and the function returns one selected value rather than a new collection.
Scanning for an Extreme
To understand the result, imagine Python examining the values in a collection and keeping track of the current largest or smallest value. With max(), a value replaces the current choice when it is larger. With min(), a value replaces the current choice when it is smaller. After the collection has been considered, the final choice is returned as one value.
Finding Extremes in a Numeric List
Consider the numeric list [3, 8, 2, 5]. What values should max() and min() return?
- 1Largest value
Compare the numbers by their numeric values. Among 3, 8, 2, and 5, the largest number is 8.
- 2Smallest value
Compare the same numbers by their numeric values. The smallest number is 2.
- 3Function results
max()returns the single value 8, whilemin()returns the single value 2.
max() produces 8 and min() produces 2.
Numbers Follow the Number Line
When max() and min() receive numbers, Python compares their numeric values. The largest number is the maximum, and the smallest number is the minimum. This is usually intuitive because the ordering follows the number line: values farther toward the larger end can become the result of max(), while values farther toward the smaller end can become the result of min().
| Function | Question it answers | Result for [3, 8, 2, 5] |
|---|---|---|
| max() | Which number is largest? | 8 |
| min() | Which number is smallest? | 2 |
Both functions return one value from the numeric collection.
Characters Have an Ordering
max() and min() also work with strings. For character comparison, Python follows a standard ordering in which spaces come before uppercase letters, and uppercase letters come before lowercase letters. Therefore, character ordering is not always the same as the alphabetical order people use when organizing words.
What do you think happens?
For the string "Hello world", which character should max() return, and which character should min() return?
Reveal answer
max() returns w and min() returns the space.
The space comes before uppercase letters and lowercase letters, so it is the smallest character in this string. Lowercase letters come after uppercase letters, and w is the greatest character present.
The important distinction is that max() and min() use Python's comparison ordering. They do not apply a separate idea of alphabetical order. In a string such as "Hello world", the space can be the minimum, while a lowercase character can be the maximum.
Mistakes with Results and Collections
Common mistake Forgetting that the result is one value
What it looks likeTreating max() or min() as though it returns a new collection
Why it failsThese functions return a single value selected from the collection.
Do this insteadUse the returned value as the largest or smallest item, rather than expecting another collection.
Common mistake Calling max() or min() on an empty collection
What it looks likeTrying to find a largest or smallest value when there are no items
Why it failsThere is no value available to select as the maximum or minimum.
Do this insteadMake sure the collection contains a value before asking for its maximum or minimum.
Common mistake Assuming character ordering is ordinary alphabetical order
What it looks likeExpecting an uppercase character to be greater than every lowercase character
Why it failsPython's stated ordering places spaces first, then uppercase letters, then lowercase letters.
Do this insteadUse Python's character ordering when predicting max() and min() results for strings.
Choose the Correct Function
For each collection, decide whether max() or min() is appropriate, then identify the returned value: the numeric list [14, 6, 21, 9], and the string "Code".
Hints
- For the numeric list, compare the values using their numeric ordering.
- For the string, remember that uppercase letters come before lowercase letters.
- Each function returns one value, not a new collection.
Checking the Practice Results
Determine the maximum and minimum for [14, 6, 21, 9], then determine the maximum and minimum character for "Code".
- 1Numeric collection
The largest number in [14, 6, 21, 9] is 21, and the smallest number is 6.
- 2String collection
The string contains uppercase C and lowercase o, d, and e. Uppercase letters come before lowercase letters, and among the lowercase letters, o is greatest while d is smallest.
- 3Returned values
The numeric results are 21 for
max()and 6 formin(). The string results are o formax()and C formin().
For the list, max() returns 21 and min() returns 6. For "Code", max() returns o and min() returns C.
The Working Rule
- 1
max()finds and returns the largest value in a collection. - 2
min()finds and returns the smallest value in a collection. - 3For numbers, Python compares numeric values.
- 4For strings, Python follows the ordering space, uppercase letters, then lowercase letters.
- 5Both functions return one selected value rather than a new collection.
Key Takeaways
- Pass a collection to
max()ormin()as an argument. max()returns the largest value, whilemin()returns the smallest value.- Numeric comparisons follow numeric ordering.
- String comparisons use Python's ordering of spaces, uppercase letters, and lowercase letters.
- Always expect a single returned value and check that the collection is not empty.