some_list = []
some_list.append(5)
some_list.append(6)
some_list.append(7)
print some_list
for item in some_list:
print item
[5, 6, 7] 5 6 7
# Write Python code to calculate the average of the numbers in some_list; it should work for any set of numbers in some_list.
average = 0.
for num in some_list:
average += num
average = average / len(some_list)
print average
6.0
Points to discuss:
# for example, combining lists [] and dicts {} can be useful:
letters_list = ['a', 'b', 'c', 'd', 'e']
letters_dict = {}
for i in range(len(letters_list)):
single_letter = letters_list[i]
letters_dict[single_letter] = i
some_list = ["duck", "goose", "mouse", "mouse", "mouse", "duck"]
counts = {}
counts["duck"] = 0
for item in some_list:
if item == "duck":
counts["duck"] += 1
## Write Python code to generically count the number of times *each* item in some_list is there.
## The output should be in dictionary format, e.g. 'print counts["duck"]' should show '2'.
counts = {}
# first, set counts[item] to 0 for all possible items
for item in some_list:
counts[item] = 0
# next, count!
for item in some_list:
counts[item] += 1
print counts
{'goose': 1, 'mouse': 3, 'duck': 2}
Points to discuss:
two loops is unnecessary; you could use
counts[item] = counts.get(item, 0) + 1
as an alternative in a single loop.
some_list actually has a 'count' function, so you could do:
for item in some_list: counts[item] = some_list.count(item)
but this goes over each item multiple times. (put a print statement in to see this) What you really would want to do is
for item in set(some_list): counts[item] = some_list.count(item)
In practice the dictionary approach using counts.get is probably the fastest - it only goes over the list once. (This matters when you have really really long lists.)
If we have time, I'll discuss 2N vs $N^2$.
Takeaway: TANSTAAFL (There Ain't No Such Thing As A Free Lunch): you need to assume the computer doesn't know what you're trying to do, so cannot do it efficiently for you.
Functions encapsulate code to make it reusable by making it generic and labelling it.
def add_two_numbers(a, b):
c = a + b
return c
add_two_numbers(5, 10)
15
Write functions for one or both of the above exercises; e.g.
# make the below code work by defining a new function, 'average'
def average(x):
total = 0.
for item in x:
total += item
return total / len(x)
some_list = []
some_list.append(5)
some_list.append(6)
some_list.append(7)
print average(some_list)
6.0
def count_items(x):
d = {}
for item in x:
d[item] = d.get(item, 0) + 1
return d
some_list = ["duck", "goose", "mouse", "mouse", "mouse", "duck"]
counts_dict = count_items(some_list)
print "duck shows up", counts_dict["duck"], "times"
duck shows up 2 times
When you're done, save the file and run the following cell. It will give you a URL like 'https://gist.github.com/7036805'. Take the number at the end and go to 'http://nbviewer.ipython.org' and enter it into the box; hit enter. You should see a static version of your notebook. Send me that URL as part of your homework hand-in (e.g. http://nbviewer.ipython.org/7036805).
# !gist class3-lists-dicts-functions.ipynb