3: Lists

Book ref: Project 6 (pg 149ff)

Python 2.7: Mostly same, see note below

See also: Python 3/Project 6 post

Source: Python for kids

Python4kids
You have learned how to give a literal a name, like this:

name = "Brendan"

or

age = 8

Giving it a name allowed you to identify the value in the future. While strictly incorrect, people sometimes say that you are “storing” the value in the name – and they even call the name a varable.
There is a bit of a problem with this because you have to name each of the values you have one at a time.
Imagine you had a group of 3 people and your teacher told you you had to write a program to store all of their ages and to find their average age. Let’s say their ages are 8, 13 and 25. How would you do it?

Well, you could try something like this:

person1_age = 8
person2_age = 13
person3_age = 25

average_age = (person1_age+person2_age+person3_age)/3

print("average age is ", average_age)

That works,* but… The next day your teacher comes and tells you that someone else has joined and you now need to include their age (17) in your program. How do you do it?

Well, you could do this:

person1_age = 8
person2_age = 13
person3_age = 25
person4_age = 17 

average_age = (person1_age+person2_age+person3_age+person4_age)/4

print("average age is ", average_age)

To add a single extra age to your calculation you made three additions. You added a line person4_age = 17 to store the age and then you changed the average_age line to add that new variable to the calculation and also you need to keep track of the number of people and change the divided by 3 to divided by 4. The next day another person, aged 11 comes along, and you use the same way of dealing with the problem. You end up with this code:

person1_age = 8
person2_age = 13
person3_age = 25
person4_age = 17
person5_age = 11

average_age = (person1_age+person2_age+person3_age+person4_age+person5_age)/5

print("average age is ", average_age)

I hope that you’re dreading any more people arriving – but it can get much worse without any more people! Imagine your teacher asks you to write code to print out the ages in order from smallest to largest. The only way you can do that with this code is to sort the ages yourself and then print the variables in the correct order. What’s the point of learning Python if you have to do the calculations yourself?

Luckily Python has a way of storing these values that makes doing things like this easy. It’s called a list. You can create a list of values by putting a comma between each pair of values and surrounding the whole lot by square brackets []. For example, say your values are 1 and 4, you would create a list like this:

>>> [1, 4]
[1, 4]

There’s a comma in between them and they’re surrounded by square brackets []. You can add more entries in the list by adding a comma then the new entry:

>>> [1, 4, 8]
[1, 4, 8]

A list can be empty – contain no values. It looks like this:

>>> []
[]

A list with only one value looks like this (no commas):

>>> [1]
[1]

You name a list in the same way you name any other value:

>>> a_list = [1, 2, 3, 4]

So, for the ages you had above you could make a list like this:

>>> ages = [8, 13, 25, 17, 11]
>>>

In Python lists can sort themselves. You just add .sort() to the name of the list and it will become sorted. Like this:

>>> ages.sort()
>>> ages
[8, 11, 13, 17, 25]

Python calls the number of entries in a list its length. To get this number you use the len() facility. Like this:

>>> len(ages)
5

This says that the list called ages has 5 entries in it (count them yourself to confirm it’s correct).

You can add all of the entries in a list by using the sum() facility.

>>> sum(ages)
74

So, you can simplify your average age program above like this:

ages = [8, 13, 25, 17, 11]
print ("average age is ", sum(ages)/len(ages))

Amazing? It’s certainly much simpler. Test that it works for any number of ages by adding and removing ages to the ages list. If the age list is empty you’ll break the code. Try it and see.

You can tackle the requirement to put them in order very easily as well:

ages = [8, 13, 25, 17, 11]
print ("average age is ", sum(ages)/len(ages))
ages.sort() 
print ("ages in order are: ", ages)

Aside:
You didn’t notice, but the .sort() facility on the list ages is unusual because it changes the list itself. In Python it is more usual for these facilities to leave the thing they are working on unchanged and to return a new value for you. Normally you can count on the thing you started with not changing.

* Note: as you learned in Python for Homework Python 2.7 carries out division differently from Python 3. For Python 2.7 it is better to use float(3) or 3.0, otherwise Python will round the result. See Python for Homework for more details.