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:
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:
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:
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
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:
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:
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.
- Run the starting code below. Write down your total.
- Change your
totalline tototal = sum(expenses). Run it. Confirm your total matches the one you wrote down. - Add a fifth expense: put
, 800before the closing]on yourexpensesline, so the list starts with four amounts. Run it. ConfirmExpenses recordedwent up by one and your total went up by 800. The old hand-written line would have missed it. - Under the
totalline, addbiggest = max(expenses)andsmallest = min(expenses). At the end of the file, addprint(f"Biggest spend: ₦{biggest:,}")andprint(f"Smallest spend: ₦{smallest:,}"). Run it. Confirm the two new lines show the largest and smallest amounts in your list. - Break it on purpose: change
expenses.append(fuel)toexpenses.append(fuel_receipt). Run it. Confirm the console shows aTypeErrorthat mentions'int' and 'str', pointing at yourtotalline. - Fix it: change
fuel_receiptback tofuel. Run it. Confirm your full report returns.
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.