Your tracker has one variable per expense. This lesson moves your expenses into a single list, so a new expense no longer needs a new name.
The concept
Here is your tracker as the last lesson left it. Run it:
Four names, and a total line that names each one. Now run this:
The first line printed all three amounts at once, inside square brackets. They are stored under one name, expenses, in the order you wrote them. A value that holds other values in order is a list. The second line printed 3: len() is a built-in function that counts the items in a list.
You can pick one item out of a list by its position:
expenses[0] gave 1500, the first item. Python counts positions from 0, not from 1. This trips up almost everyone at first. In a list of three items, the positions are 0, 1 and 2.
The last line used -1. A negative position counts from the end, so expenses[-1] is always the last item, however long the list is.
A list can grow:
expenses.append(3500) added 3500 to the end of the list. The list now has three items, and len agrees.
append is written after the list's name and a dot. A function that belongs to a value like this is called a method. Here, append belongs to the list and changes that list.
The syntax
expenses = [1500, 2500, 3500]Square brackets hold the items. A comma separates each item from the next.
first = expenses[0]
last = expenses[-1]
count = len(expenses)
expenses.append(4200)expenses[0]is the item at position 0. The number in square brackets is called the index.expenses[-1]is the last item.len(expenses)gives back how many items the list holds.expenses.append(4200)adds one value to the end. Write it on a line of its own.
A minimal working example
expenses = [1500, 2500, 3500]
expenses.append(4200)
count = len(expenses)
print(expenses)
print(f"Expenses recorded: {count}")Output: [1500, 2500, 3500, 4200], then Expenses recorded: 4.
Common mistakes
You ask for a position past the end. Run this:
IndexError: list index out of rangeThree items sit at positions 0, 1 and 2. Position 3 does not exist. When you see this error, count from 0 and check the last position the list really has.
You leave out a comma between items. [1500 2500] gives SyntaxError: invalid syntax. Perhaps you forgot a comma? This time Python's guess is right. Add the comma.
You use round brackets to pick an item. Run this:
TypeError: 'list' object is not callableRound brackets after a name mean "call this", the way print(...) calls print. A list is not something you can call. Use square brackets to pick an item: expenses[0].
You store what append gives back. expenses = expenses.append(4200) looks reasonable, but print(expenses) then shows None. append changes the list and gives back nothing, and None is Python's word for nothing. That line replaced your list with None. Write expenses.append(4200) on its own line.
Try it yourself
You will move your expenses into a list without changing your total.
- Run the starting code below. Write down your total.
- Under the
fuel = int(fuel_receipt)line, addexpenses = [1500, 2500, 3500]. Use your ownbus_fare,lunchanddata_bundleamounts, in that order. Addprint(expenses)as the very last line of the file. Run it. Confirm the last line shows your three amounts in square brackets. - Under the
expensesline, addexpenses.append(fuel). Run it. Confirm the list now has four amounts, with your fuel amount last. - Change your
totalline tototal = expenses[0] + expenses[1] + expenses[2] + expenses[3]. Then delete thebus_fare,lunchanddata_bundlelines. Run it. Confirm your total matches the one you wrote down. - Under the
appendline, addcount = len(expenses). Above yourTotal spentline, addprint(f"Expenses recorded: {count}"). Delete theprint(expenses)line. Run it. Confirm the report showsExpenses recorded: 4above your total. - Break it on purpose: in your
totalline, changeexpenses[3]toexpenses[4]. Run it. Confirm the console showsIndexError: list index out of range, pointing at yourtotalline. - Fix it: change the
4back to3. Run it. Confirm your report returns.
Your expenses live in one list now. The total line still names every position by hand, though, so a fifth expense would be left out of the total. The next lesson replaces that line with one function that reads the whole list.