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?
Largest value: Compare the numbers by their numeric values. Among 3, 8, 2, and 5, the largest number is 8.
Smallest value: Compare the same numbers by their numeric values. The smallest number is 2.
Function results: max() returns the single value 8, while min() 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 |
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
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
Forgetting that the result is one value
These functions return a single value selected from the collection.
Fix:
Use the returned value as the largest or smallest item, rather than expecting another collection.Calling max() or min() on an empty collection
There is no value available to select as the maximum or minimum.
Fix:
Make sure the collection contains a value before asking for its maximum or minimum.Assuming character ordering is ordinary alphabetical order
Python's stated ordering places spaces first, then uppercase letters, then lowercase letters.
Fix:
Use 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".
Numeric collection: The largest number in [14, 6, 21, 9] is 21, and the smallest number is 6.
String 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.
Returned values: The numeric results are 21 for max() and 6 for min(). The string results are o for max() and C for min().
For the list, max() returns 21 and min() returns 6. For "Code", max() returns o and min() returns C.
The Working Rule
- max() finds and returns the largest value in a collection.
- min() finds and returns the smallest value in a collection.
- For numbers, Python compares numeric values.
- For strings, Python follows the ordering space, uppercase letters, then lowercase letters.
- Both functions return one selected value rather than a new collection.
Key Takeaways
- Pass a collection to max() or min() as an argument.
- max() returns the largest value, while min() 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.