Concepts / Introduction to Functions and Return Values

Introduction to Functions and Return Values

Initialize the extreme variable to None before the loop to mark it as empty.

  • Programming

Why Extreme Values Matter

Many programs need to find an extreme value in a collection of data. A weather application might find the highest temperature recorded during a week. A sales system might identify the lowest-priced item in inventory. A fitness tracker might find the maximum heart rate during a workout. These tasks follow the same basic idea: examine each value and remember the largest or smallest value seen so far.

The central pattern is to maintain one variable that represents the extreme value found so far, then update it only when a new value is more extreme.

Tracking the Largest Value

For a maximum loop, the tracking variable represents the largest value encountered up to the current iteration. The comparison has two parts: largest is None handles the empty starting state, and itervar > largest checks whether the current value is larger than the saved value. The saved value changes only when one of those conditions is true.

compare 4compare 9compare 2compare 11largestNonelargest4largest9largest9largest11
How does the largest-so-far variable change as each value is compared?

Finding a maximum step by step

Apply the largest-so-far pattern to the sequence 4, 9, 2, 11.

Start: Set largest to None because no sequence value has been examined yet.

Read 4: largest is None, so largest becomes 4.

Read 9: 9 is greater than 4, so largest becomes 9.

Read 2: 2 is not greater than 9, so largest remains 9.

Read 11: 11 is greater than 9, so largest becomes 11.

The largest value found is 11.

Writing the Maximum Pattern

python

The initialization occurs before the loop. Each iteration examines itervar. If largest is still None, the current value becomes the first saved value. Otherwise, the current value replaces largest only when it is strictly greater. A value equal to the current maximum does not change the tracking variable.

Tracking the Smallest Value

The minimum pattern has the same structure as the maximum pattern. The variable begins as None, the first value fills the empty state, and later values are compared with the saved extreme. The only comparison change is the operator: a minimum loop uses itervar < smallest instead of itervar > largest.

compare 8compare 3compare 6compare 1smallestNonesmallest8smallest3smallest3smallest1
How does the smallest-so-far variable change as each value is compared?
python

Finding a minimum step by step

Apply the smallest-so-far pattern to the sequence 8, 3, 6, 1.

Start: Set smallest to None because no sequence value has been examined yet.

Read 8: smallest is None, so smallest becomes 8.

Read 3: 3 is less than 8, so smallest becomes 3.

Read 6: 6 is not less than 3, so smallest remains 3.

Read 1: 1 is less than 3, so smallest becomes 1.

The smallest value found is 1.

Starting with None

None marks the extreme variable as empty before the loop has examined any value. On the first iteration, the condition involving None allows the first sequence value to become the initial maximum or minimum. After that, ordinary greater-than or less-than comparisons control updates.

first valuelarger value foundlargestNonelargest4largest11
How does an extreme variable change from None to the first value and then to a valid extreme?

Using Functions for Extreme Values

Python provides the built-in max() and min() functions for finding extreme values. In real-world code, use these functions when the task is simply to find the maximum or minimum. They are more concise, faster, and less error-prone than writing the loop yourself. The manual loop remains important because it exposes the underlying algorithm, supports debugging, and transfers to languages that do not provide built-in extreme functions.

python
sequencecomputed valuevaluessequencemax()extreme-finding functionhighestmaximum value
How does an extreme value computed by a function become available to the code that uses the result?

Mistakes in Extreme Loops

  • Initializing the tracking variable to an arbitrary value.

    The chosen starting value may not represent an actual sequence value and can interfere with finding the correct extreme.

    Fix: Initialize the extreme variable to None, then let the first value establish the starting point.

  • Using the wrong comparison operator.

    A minimum loop must replace the saved value only when the new value is smaller.

    Fix: Use itervar < smallest for the minimum pattern.

  • Updating the extreme on every iteration.

    The saved value would become the most recently examined value rather than the largest value seen so far.

    Fix: Update only when the current value is strictly more extreme.

  • Writing a manual loop when a built-in function is sufficient.

    The manual version is longer and more prone to errors.

    Fix: Prefer the built-in function in real-world code when no additional loop behavior is required.

Practice the Pattern

MEDIUM

For the sequence 12, 5, 18, 18, 7, trace the value of largest after every iteration. Then write the condition for a smallest-so-far loop and trace smallest for the same sequence.

Hints
  • Begin each tracking variable as None.
  • For largest, update only when the current value is greater than the saved value.
  • For smallest, update only when the current value is less than the saved value.
  • An equal value does not change the tracking variable.

Key Takeaways

  1. A maximum loop maintains the largest value seen so far and uses the greater-than operator.
  2. A minimum loop maintains the smallest value seen so far and uses the less-than operator.
  3. None marks the tracking variable as empty before the first sequence value is examined.
  4. The tracking variable changes only when a strictly more extreme value appears.
  5. Use built-in max() and min() for ordinary extreme-finding tasks, but learn the manual loop to understand the algorithm and debug it.

Key Takeaways

  • Extreme-finding loops compare each value with the best value found so far.
  • Use None as the initial state so the first value can establish the maximum or minimum.
  • Maximum loops use >, while minimum loops use <.
  • A tracking variable remains unchanged when the new value is equal to or less extreme than the saved value.
  • Prefer max() and min() for straightforward real-world tasks, while understanding the manual pattern for algorithmic insight and debugging.