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.
print (5 + 6 + 7)/3.
some_list=[1,2]
print some_list
total = 0.0
for item in some_list:
total = total + item
average = total / len(some_list)
print average
print float(sum(some_list))/len(some_list)
import numpy
print numpy.mean(some_list)
6.0 [1, 2] 1.5 1.5 1.5
# 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'.
n_ducks = 0
n_mice=0
n_geese=0
for item in some_list:
if item == "duck":
n_ducks += 1
### ^^^ that's how you'd do it if you weren't using dictionaries
counts = {}
for item in some_list:
counts[item] = 0
for item in some_list:
counts[item] = counts[item] + 1
print counts
{'goose': 1, 'mouse': 3, 'duck': 2}
# identical to ^^^
counts = {}
for item in some_list:
counts[item] = counts.get(item, 0) + 1
print counts
{'goose': 1, 'mouse': 3, 'duck': 2}
counts = {}
for item in some_list:
n = 0
for item2 in some_list:
if item == item2:
n += 1
counts[item] = n
# equivalent to
#counts[item] = some_list.count(item)
print counts
{'goose': 1, 'mouse': 3, 'duck': 2}
s = "ATGGACCAGATGGACACAGATTAGAGAGA"
counts = {}
for ch in s:
print 'at', ch, 'counts is', counts.get(ch, 0)
counts[ch] = counts.get(ch, 0) + 1
print counts
counts2 = {}
K = 4
for i in range(len(s) - K + 1):
word = s[i:i+K]
counts2[word] = counts2.get(word, 0) + 1
print counts2
at A counts is 0 at T counts is 0 at G counts is 0 at G counts is 1 at A counts is 1 at C counts is 0 at C counts is 1 at A counts is 2 at G counts is 2 at A counts is 3 at T counts is 1 at G counts is 3 at G counts is 4 at A counts is 4 at C counts is 2 at A counts is 5 at C counts is 3 at A counts is 6 at G counts is 5 at A counts is 7 at T counts is 2 at T counts is 3 at A counts is 8 at G counts is 6 at A counts is 9 at G counts is 7 at A counts is 10 at G counts is 8 at A counts is 11 {'A': 12, 'C': 4, 'T': 4, 'G': 9} {'ACAG': 1, 'GGAC': 2, 'ACAC': 1, 'ATGG': 2, 'TTAG': 1, 'TGGA': 2, 'GATT': 1, 'GAGA': 2, 'ACCA': 1, 'GACA': 1, 'CACA': 1, 'TAGA': 1, 'ATTA': 1, 'AGAT': 2, 'CAGA': 2, 'AGAG': 2, 'GACC': 1, 'GATG': 1, 'CCAG': 1}
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)
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(the_list):
total = 0.0
for item in the_list:
total = total + item
result = total / len(the_list)
return result
some_list = []
some_list.append(5)
some_list.append(6)
some_list.append(7)
print average(some_list)
some_list
8.25
[5, 6, 7, 15]
def count_items(the_list):
counts = {}
for item in some_list:
counts[item] = 0
for item in some_list:
counts[item] = counts[item] + 1
return counts
some_list = ["duck", "goose", "mouse", "mouse", "mouse", "duck"]
counts_dict = count_items(some_list)
print counts_dict
print "duck shows up", counts_dict["duck"], "times"
{'goose': 1, 'mouse': 3, 'duck': 2} 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
https://gist.github.com/7036800