Summarize Existing Sequences

Replace understood loops with sum, min, max, any, and all while keeping their empty-input behavior and evidence limits visible.

Chapter 3 built a total and count one reading at a time. That loop made every update visible. Once we understand the calculation, a standard Python function can state the same operation more directly.

Compare sum with the Loop It Replaces

Start with four available readings:

The loop produces 82. The built-in sum function produces the same result from the existing list:

sum does not change readings. It visits the values and returns one result. We can check the compact form against the loop before relying on it:

assert built_in_total == loop_total

Q1. Match a summary to its loop

What does sum([4, 7, 9]) return?

Compute it first, then check your number.

HintUse the visible operation

Calculate 4 + 7 + 9.

SolutionThe total is twenty

The three values add to 20, so sum([4, 7, 9]) returns 20.

Not attempted
Review

Not marked done.

Read the Smallest and Largest Values

min and max summarize the bounds of a non-empty sequence:

Their loop equivalents keep a current candidate and replace it when a smaller or larger value appears:

The first item supplies a real candidate. The loop then checks every remaining item. The built-ins hide that familiar repetition, not the meaning of the result.

An empty list has a total of zero in Python:

sum([])  # 0

It has no smallest or largest item. Calling min([]) or max([]) raises a ValueError. Check that a sequence is non-empty before asking for a bound when empty input is possible:

Q2. Respect the empty-input boundary

Which expression has a defined numerical result for an empty list?

Choose one

Select one choice, then check.

HintAsk what value could be selected

min and max must return an item from the sequence, but an empty sequence has no item.

SolutionOnly the sum is defined here

Python defines sum([]) as 0. min([]) and max([]) raise ValueError because there is no candidate value to return.

Not attempted
Review

Not marked done.

Summarize Existing True-or-False Values

any asks whether at least one value is true. all asks whether every value is true. We can first build the checks with an ordinary loop:

The list keeps the individual evidence visible. Ordinary loops show the two questions directly:

any(validity) follows the first loop and returns as soon as it finds a true value. all(validity) follows the second and returns as soon as it finds a false value.

Their empty results follow those questions:

The result of all([]) can look surprising. It does not claim that an empty dataset contains valid readings. It only says that no item in the supplied sequence failed the test. If empty data has a separate meaning, check its length separately.

Q3. Check several existing results

Complete the program so it displays True, False, and 82.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintMatch each question to one function

Use all(validity), any(high_flags), and sum(readings) in that order.

SolutionSummarize the three sequences
Not attempted
Review

Not marked done.

sum, min, max, any, and all give familiar loops concise names when their inputs already exist. The next lesson will build a complete transformed list, first with a visible loop and then with a comprehension.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 3 exercises marked done

Review

Not marked done.