CS150 - Fall 2011 - Class 9

• http://www.youtube.com/watch?v=NHWjlCaIrQo

• quiz!

• class survey:
- http://www.surveymonkey.com/s/C6N8NJL

• admin
- no digital submission
- extra tutor hours: M-Th 9-11pm

• run scores-lists.py code
- First, prompts user to enter a list of scores one at a time
- how is this done?
- while loop
- what is the exit condition?
- checks to see if the line is empty
while line:
...

remember that the empty string counts as False
- could have made this more explicit with:

while line != ""
- then, calculates various statistics based on what was entered
- how are we calculating these statistics?
- average?
- could keep track of the sum and the number of things entered
- divide at the end
- max?
- keep track of the largest seen so far
- each time a new one is entered, see if it's larger, if so, update the largest
- min?
- same thing
- median?
- the challenge with median is that we can't calculate it until we have all of the scores
- need to sort them and then find the middle score

- why can't we do this using int/float variables?
- we don't know how many scores are going to be entered
- even if we did, if we had 100 students in the class, we'd need 100 variables!

• lists
- lists are a data structure in Python
- what is a data structure?
- a way of storing and organizing data

- lists allow us to store multiple values with a single variable

• creating lists: we can create a new list using the square brackets
>>> [7, 4, 3, 6, 1, 2]
[7, 4, 3, 6, 1, 2]
>>> 10 # not a list
10
>>> l = [7, 4, 3, 6, 1, 2]
>>> l
[7, 4, 3, 6, 1, 2]
>>> type(l)
<type 'list'>

lists are a type and represent a value, just like float, ints and strings. We can assign them to variables, print them, etc.

- what do you think [] represents?
- empty list
>>> []
[]

• accessing lists: lists support most of the same functionalities of strings
- we can get at particular values in the list

>>> l[3]
6
>>> l[0]
7
>>> l[-1]
2
>>> type(l[3])
<type 'int'>

- notice that the values here are ints (unlike when we use indexing for strings, where the values are strings)
- we can also "splice" lists, just like we did with strings

>>> l[2:4]
[3, 6]

- draw the list representation
- a list is a contiguous set of spaces in memory
- we can store anything in each of these spaces

>>> ["this", "is", "a", "list", "of", "strings"]
['this', 'is', 'a', 'list', 'of', 'strings']
>>> list_of_strings = ["this", "is", "a", "list", "of", "strings"]
>>> list_of_strings[0]
'this'
>>> list_of_strings[0][1]
'h'
>>> list_of_strings[1].toupper()
'IS'
>>> list_of_strings[1:4]
['is', 'a', 'list']
>>> [1, 5.0, "my string"]
[1, 5.0, 'my string']
>>> l = [1, 5.0, "my string"]
>>> type(l[0])
<type 'int'>
>>> type(l[1])
<type 'float'>
>>> type(l[2])
<type 'str'>

in general, you should try and avoid non-homogenous lists since there are often better ways of putting things of different types together

• lists are mutable
- what does that mean?
- we can change (or mutate) the values in a list

>>> my_list = [15, 2, 1, 20, 5]
>>> my_list
[15, 2, 1, 20, 5]
>>> my_list[2] = 100
>>> my_list
[15, 2, 100, 20, 5]

• lists are objects and therefore have methods. Any guesses?
- append: add a value on to the end of the list
>>> my_list = [15, 2, 1, 20, 5]
>>> my_list.append(100)
>>> my_list
[15, 2, 1, 20, 5, 100]

- notice that append does NOT return a new list, it modifies the existing list!
- try some out on your own:
http://docs.python.org/tutorial/datastructures.html

- pop: remove a value off of the end of the list and return it
>>> my_list.pop()
100
>>> my_list
[15, 2, 1, 20, 5]

- notice that it both modifies the list and returns a value
- if you want to use this value, you need to store it!
>>> x = my_list.pop()
>>> x
5
- pop also has another version where you can specify the index

>>> my_list = [15, 2, 1, 20, 5]
>>> my_list.pop(2)
1
>>> my_list
[15, 2, 20, 5]
- insert: inserts a value at a particular index
>>> my_list = [15, 2, 1, 20, 5]
>>> my_list.insert(2, 100)
>>> my_list
[15, 2, 100, 1, 20, 5]

- again, lists are mutable, so insert does not return a new list, but modifies the underlying one
- sort
>>> my_list = [15, 2, 1, 20, 5]
>>> my_list.sort()
>>> my_list
[1, 2, 5, 15, 20]
>>> my_list = ["these", "are", "some", "words", "to", "sort"]
>>> ["these", "are", "some", "words", "to", "sort"].sort()
>>> my_list = ["these", "are", "some", "words", "to", "sort"]
>>> my_list.sort()
>>> my_list
['are', 'some', 'sort', 'these', 'to', 'words']

• built-in functions over lists: there are also some built-in functions that take a list as a parameter
- we can get the length of a list, similar to strings
>>> len(l)
3
>>> len([1, 2, 3, 4, 5])
5
>>> len([])
0
- max
>>> l = [5, 3, 2, 1, 10]
>>> max(l)
10

- min
>>> min(l)
1
- sum
>>> sum(l)
21

• range function
- we've used the range function in loops to iterate over numbers, e.g.

for i in range(10):
# do something

- what does the range function actually do?
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(15,20)
[15, 16, 17, 18, 19]
>>> range(-5, 0)
[-5, -4, -3, -2, -1]

• looping over lists
- all of our for loops so far have actually just been looping over lists

for i in range(10):
# do something

is really the same as:

some_list = range(10)

for i in some_list:
# do something

- the way to read this list is:
- for each element in the list, do something
- for each iteration of the loop, i (or whatever variable you put there) will get the next value in the list

>>> my_list = [4, 1, 8, 10, 11]
>>> for i in my_list:
...    print i
...
4
1
8
10
11

• back to our grades program: look at scores-lists.py code
- there is a function called get_scores. That gets the scores and returns them as a list. How?
- starts with an empty list
- uses append to add them on to the end of the list
- returns the list when the loop finishes
- average function
- has a single parameter, but this parameter will represent a list
- inelegant_averate
- calculates the sum and divides by the number of entries
- uses a for loop to iterate over the values
- often, we'll use something besides "i" as a variable name that makes our program more readable
- is there a better way to do this?
- look at fancy_average
- us the sum function over lists
- median function
- sorts the values
- notice again that sort does NOT return a value, but sorts the list that it is called on
- returns the middle entry