Introduction to Functions and Return Values
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.
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.
Finding a maximum step by step
Apply the largest-so-far pattern to the sequence 4, 9, 2, 11.
- 1Start
Set largest to None because no sequence value has been examined yet.
- 2Read 4
largest is None, so largest becomes 4.
- 3Read 9
9 is greater than 4, so largest becomes 9.
- 4Read 2
2 is not greater than 9, so largest remains 9.
- 5Read 11
11 is greater than 9, so largest becomes 11.
The largest value found is 11.
Writing the Maximum Pattern
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.
Finding a minimum step by step
Apply the smallest-so-far pattern to the sequence 8, 3, 6, 1.
- 1Start
Set smallest to None because no sequence value has been examined yet.
- 2Read 8
smallest is None, so smallest becomes 8.
- 3Read 3
3 is less than 8, so smallest becomes 3.
- 4Read 6
6 is not less than 3, so smallest remains 3.
- 5Read 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.
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.
Mistakes in Extreme Loops
Common mistake Initializing the tracking variable to an arbitrary value.
What it looks likelargest = 0Why it failsThe chosen starting value may not represent an actual sequence value and can interfere with finding the correct extreme.
Do this insteadInitialize the extreme variable to None, then let the first value establish the starting point.
Common mistake Using the wrong comparison operator.
What it looks likeif smallest is None or itervar > smallest:Why it failsA minimum loop must replace the saved value only when the new value is smaller.
Do this insteadUse itervar < smallest for the minimum pattern.
Common mistake Updating the extreme on every iteration.
What it looks likelargest = itervarWhy it failsThe saved value would become the most recently examined value rather than the largest value seen so far.
Do this insteadUpdate only when the current value is strictly more extreme.
Common mistake Writing a manual loop when a built-in function is sufficient.
What it looks likeUse max() or min() for a direct extreme-finding task.Why it failsThe manual version is longer and more prone to errors.
Do this insteadPrefer the built-in function in real-world code when no additional loop behavior is required.
Practice the Pattern
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
- 1A maximum loop maintains the largest value seen so far and uses the greater-than operator.
- 2A minimum loop maintains the smallest value seen so far and uses the less-than operator.
- 3None marks the tracking variable as empty before the first sequence value is examined.
- 4The tracking variable changes only when a strictly more extreme value appears.
- 5Use built-in
max()andmin()for ordinary extreme-finding tasks, but learn the manual loop to understand the algorithm and debug it.
At a glance
- 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()andmin()for straightforward real-world tasks, while understanding the manual pattern for algorithmic insight and debugging.