So far, one variable held one value. A class has thirty marks, a shop has a hundred prices, and you cannot make a hundred variables. A list holds many values in one variable. In this course you make lists, read them by position and by slice, add, remove, replace and sort items, go through a list with a loop, and find totals, averages, the biggest and the smallest. You learn why a copy of a list is not what you expect, and how to make a table with a list inside a list. A marks tracker is the project at the end. The practice runs your code on real lists and tells you which results are wrong.
5 lessons, about 2 h 40 min5 simulations15 problems10 practice questionsPython in every lesson
A list holds many values in one variable. It is written in square brackets with commas between the values. Positions start at 0, and negative positions count from the end. A slice takes a piece and stops before the last position. len() gives the number of items, and in asks whether a value is in the list. A list can hold numbers, text, True and False, a mix of them, or nothing at all. Asking for a position that does not exist gives an IndexError.
A list can be changed. append adds an item at the end, insert puts one at a position, remove deletes the first match, pop takes the last item out and gives it back, and a position on the left of an equals sign replaces an item. sort puts the items in order and reverse turns them around, and both change the list itself and give back None. sorted gives a new sorted list and keeps the original. A string cannot be changed, and trying to do so gives a TypeError.
A for loop goes through a list, one item in each round, in the same way that it goes through a text. A loop can build a total, count the items that pass a test, and build a new list by starting with an empty list and using append. sum, max and min give quick answers, and the average is sum divided by len. To use the position as well as the item, loop over range of len, or use enumerate. Two lists in the same order work like a table.
A variable holds a pointer to a list, not the list itself. After b = a, both names point to the same list, so a change through one shows in the other, and is tells you whether two names are the same list. To make a real copy, use a.copy(), a[:] or list(a). A list given to a function is not copied either, so the function can change your list. A list can hold other lists, which makes a table: marks[1][2] is row 1, column 2. Loop over the rows, and then over each row.
In this project we write a marks tracker for a class. Two lists hold the names and the marks. The program works out the class size, the total, the average, the highest and the lowest mark with the name of each student, makes a list of the students who passed, and shows the marks from the highest. We plan the report first, build it in small steps, test it with a small list whose answers we know, and extend it with grades, a failed list and a new student. The project uses lists, loops, sum, max, min, index, append and sorted.
Log in to join the discussion.
Loading…