SypherTec
…
PART 1 · 07 OF 073 MIN READUPDATED 20 NOV 2026

sum, max and min: Questions About a Whole List

Your total line names every position by hand. This lesson replaces it with sum, and adds your biggest and smallest spends with max and min.

The concept

Here is your tracker as the last lesson left it. Run it:

WHERE YOU LEFT OFF · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Look at the total line. It adds positions 0 to 3, because the list held four items when you wrote it. Watch what happens when a fifth expense arrives:

A FIFTH EXPENSE, TWO TOTALS · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Two different totals. The line written by hand still adds four positions, so the new 800 is missing from 11700. sum(expenses) gave 12500, because it adds every item in the list, however many there are.

sum is one of several built-in functions that read a whole list and answer one question about it:

THREE QUESTIONS ABOUT ONE LIST · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

sum gave the total. max gave the largest item, 4200. min gave the smallest, 1500. You met another one of these in the last lesson: len, which counts the items.

Built-in means these functions come with Python. There is nothing to set up. You write the name, put the list in the parentheses, and Python gives back the answer.

The syntax

total = sum(expenses)
biggest = max(expenses)
smallest = min(expenses)

The list goes inside the parentheses. Each function gives back one value, so store it in a variable, as you did with len.

max and min also accept separate values: max(1500, 2500) gives 2500. sum does not. sum(1500, 2500) stops with TypeError: 'int' object is not iterable. Python wanted something it could go through item by item, such as a list. Put the numbers in a list: sum([1500, 2500]).

A minimal working example

main.py
expenses = [1500, 2500, 3500, 4200]
total = sum(expenses)
biggest = max(expenses)
 
print(f"Total spent: ₦{total:,}")
print(f"Biggest spend: ₦{biggest:,}")

Output: Total spent: ₦11,700, then Biggest spend: ₦4,200. Append another expense and both lines stay correct.

Common mistakes

A text amount gets into the list. Run this:

A TEXT AMOUNT INSIDE THE LIST · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output
TypeError: unsupported operand type(s) for +: 'int' and 'str'

The list holds "4200", the text from the receipt, not a number. sum tried to add it to the other amounts and stopped. max fails on the same list with TypeError: '>' not supported between instances of 'str' and 'int', and min with the same message about '<'. Convert first, then append the converted value: fuel = int(fuel_receipt), then expenses.append(fuel).

The list is empty. Run this:

AN EMPTY LIST · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

sum printed 0, because adding nothing gives zero. max stopped with ValueError: max() iterable argument is empty. An empty list has no biggest item to give back. "Iterable" is Python's word for something it can go through item by item, like a list. min behaves the same way.

Try it yourself

You will replace your hand-written total, add a fifth expense, then report your biggest and smallest spends.

  1. Run the starting code below. Write down your total.
  2. Change your total line to total = sum(expenses). Run it. Confirm your total matches the one you wrote down.
  3. Add a fifth expense: put , 800 before the closing ] on your expenses line, so the list starts with four amounts. Run it. Confirm Expenses recorded went up by one and your total went up by 800. The old hand-written line would have missed it.
  4. Under the total line, add biggest = max(expenses) and smallest = min(expenses). At the end of the file, add print(f"Biggest spend: ₦{biggest:,}") and print(f"Smallest spend: ₦{smallest:,}"). Run it. Confirm the two new lines show the largest and smallest amounts in your list.
  5. Break it on purpose: change expenses.append(fuel) to expenses.append(fuel_receipt). Run it. Confirm the console shows a TypeError that mentions 'int' and 'str', pointing at your total line.
  6. Fix it: change fuel_receipt back to fuel. Run it. Confirm your full report returns.
TRY IT YOURSELF · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Your tracker reports a count, a total, and your biggest and smallest spends, all worked out from one list. That completes Part 1. The checkpoint next shows what your program can do, and the one thing it cannot do yet.

QUIZ · 3 QUESTIONSCheck what stuckStart the quiz
PART 1 · 07 OF 07❮❯