Comparison Operators and Logical Expressions
Initialize the extreme variable to None before the loop to mark it as empty.
Extremes in Everyday Data
Many programs need to find an extreme value: the highest temperature, the lowest inventory price, or the maximum heart rate during a workout. The general strategy is to examine each value in turn and maintain the largest or smallest value seen so far. Each new value is compared with the current extreme, and the stored extreme changes only when the new value is more extreme.
An extreme-finding loop does not need to remember every earlier comparison. It needs one variable that records the best extreme found so far.
The Largest-So-Far Pattern
To find a maximum, compare each current value with the largest value found so far. The maximum condition is largest is None or itervar > largest. The logical or has two jobs: the first part handles the empty starting state, and the second part checks whether the current value is strictly larger than the stored value. If either part is true, the largest variable is updated.
12For the sequence 8, 12, 5, 12, the stored value begins as None. The first value, 8, becomes the largest because the variable is empty. The value 12 replaces 8 because it is larger. The value 5 does not replace 12. The final 12 is equal to the current largest, not strictly larger, so the stored value remains 12.
Why None Starts the Search
None represents the empty state of the extreme variable before the loop has processed its first value. It is not treated as a candidate number. The condition largest is None detects that no value has been selected yet; after the first value is processed, the variable holds an actual value to compare.
The Smallest-So-Far Pattern
The minimum pattern has the same structure as the maximum pattern. Initialize the extreme variable to None, then update it when the current value is smaller. Its condition is smallest is None or itervar < smallest. The only comparison change is replacing the greater-than operator with the less-than operator.
11In the sequence 18, 11, 15, 11, the first value becomes the initial smallest. The value 11 replaces 18 because it is lower. The value 15 does not replace 11 because it is higher. The final 11 is equal to the current smallest, so the stored value remains unchanged.
Reading Each Comparison
The comparison controls whether the extreme changes. For a maximum, a current value equal to the largest value does not satisfy itervar > largest, so the variable stays unchanged. For a minimum, equality also leaves the variable unchanged because the condition uses itervar < smallest. The loop is therefore looking for a strictly larger or strictly smaller value.
Manual Loops and Built-ins
| Approach | How it works | When to choose it |
|---|---|---|
| Manual loop | Examines each value and updates an extreme-so-far variable | When learning the algorithm, debugging it, or working in a language without a built-in extreme function |
| max() | Performs the maximum-value task directly | When real-world Python code needs the largest value |
| min() | Performs the minimum-value task directly | When real-world Python code needs the smallest value |
12
5For ordinary Python code, use max() or min() when you simply need the extreme value. The source explains that these functions are more concise, faster, and less error-prone than writing the loop yourself. The manual pattern remains important because it exposes the underlying algorithm, supports debugging, and transfers to languages that may not provide built-in extreme functions. The built-in min() function follows the same loop pattern studied here.
Common Mistakes
Starting the extreme variable with a guessed number
The starting number may not be appropriate for the data being processed.
Fix:
Initialize the variable to None and let the first value become the initial extreme.Using the maximum comparison for the minimum task
The greater-than operator selects larger values rather than smaller ones.
Fix:
Use itervar < smallest for the minimum pattern.Updating on equality
The studied pattern updates only for a strictly larger value.
Fix:
Use itervar > largest for maximum and itervar < smallest for minimum.Forgetting the None condition
The first iteration needs to handle the empty state before a numeric comparison is made.
Fix:
Combine the initialization check and comparison with or.
Practice the State Trace
Trace the largest-so-far variable for the sequence 6, 14, 9, 14, 3. Record the value of largest after each iteration, beginning with its initial value of None.
Hints
- The first value replaces None.
- Replace largest only when the current value is strictly greater.
- An equal value leaves largest unchanged.
Write the minimum-loop condition for a variable named smallest and a current value named itervar. Then explain why the condition contains both smallest is None and itervar < smallest.
Hints
- Use the same logical structure as the maximum pattern.
- Change the comparison operator from greater-than to less-than.
- The first part handles the empty state.
Key Takeaways
- A maximum loop stores the largest value seen so far and updates it when itervar > largest.
- A minimum loop stores the smallest value seen so far and updates it when itervar < smallest.
- None marks that no value has been selected before the first iteration.
- The extreme variable changes only for a strictly larger or strictly smaller value; equality leaves it unchanged.
- Use max() and min() in ordinary Python code, but learn the manual loop to understand the algorithm and debug it.
Key Takeaways
- Extreme-finding loops maintain one largest-so-far or smallest-so-far variable.
- Initialize that variable to None so the first sequence value can establish the initial extreme.
- Use > for the maximum pattern and < for the minimum pattern.
- Equal values do not change the stored extreme in these patterns.
- Prefer max() and min() for ordinary Python code, while understanding the loop pattern for algorithmic knowledge and debugging.