Concepts / Using Functions: Arguments and Return Values

Using Functions: Arguments and Return Values

max() and min() are built-in functions that find the largest and smallest values in any collection.

  • Programming

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.

Collectionargumentmax() or min()comparisonSelected valuereturn valuepassed toreturns
How does a collection enter max() or min() as an argument, and how does the selected value come back as the return value?

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.

3current maximum8current maximum8current maximum8returned valuecompare 8compare 2compare 5
As max() examines each item in a numeric collection, how does the current largest value change until the final result is returned?
Worked example

Finding Extremes in a Numeric List

Consider the numeric list [3, 8, 2, 5]. What values should max() and min() return?

  1. 1
    Largest value

    Compare the numbers by their numeric values. Among 3, 8, 2, and 5, the largest number is 8.

  2. 2
    Smallest value

    Compare the same numbers by their numeric values. The smallest number is 2.

  3. 3
    Function results

    max() returns the single value 8, while min() returns the single value 2.

Result

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().

FunctionQuestion it answersResult 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?

  • max() returns H and min() returns w

  • max() returns w and min() returns the space

  • max() returns o and min() returns H

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.

spacesmallest hereHuppercasewgreatest herecomes beforecomes before
How does Python compare characters to decide which character is greatest or smallest in "Hello world"?

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 like

    Treating max() or min() as though it returns a new collection

    Why it fails

    These functions return a single value selected from the collection.

    Do this instead

    Use 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 like

    Trying to find a largest or smallest value when there are no items

    Why it fails

    There is no value available to select as the maximum or minimum.

    Do this instead

    Make 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 like

    Expecting an uppercase character to be greater than every lowercase character

    Why it fails

    Python's stated ordering places spaces first, then uppercase letters, then lowercase letters.

    Do this instead

    Use Python's character ordering when predicting max() and min() results for strings.

Choose the Correct Function

MEDIUM

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.
Worked example

Checking the Practice Results

Determine the maximum and minimum for [14, 6, 21, 9], then determine the maximum and minimum character for "Code".

  1. 1
    Numeric collection

    The largest number in [14, 6, 21, 9] is 21, and the smallest number is 6.

  2. 2
    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.

  3. 3
    Returned values

    The numeric results are 21 for max() and 6 for min(). The string results are o for max() and C for min().

Result

For the list, max() returns 21 and min() returns 6. For "Code", max() returns o and min() returns C.

The Working Rule

  1. 1
    max() finds and returns the largest value in a collection.
  2. 2
    min() finds and returns the smallest value in a collection.
  3. 3
    For numbers, Python compares numeric values.
  4. 4
    For strings, Python follows the ordering space, uppercase letters, then lowercase letters.
  5. 5
    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.

Related concepts

More concepts starting with U