Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53) [GCC 4.0.1 (Apple Computer, Inc. build 5363)] Type "help", "copyright", "credits" or "license" for more information. >>> # suppose we have a bunch of measurements >>> # we could use one variable for each one, like this >>> measurement1 = 45.27 >>> measurement2 = 45.26 >>> measurement3 = 45.24 >>> # But what if you have, say, 20 measurements? You don't want to have to use 20 variables >>> # So, we put them in one list >>> measurements = [45.27,45.26,45.24] >>> measurements [45.270000000000003, 45.259999999999998, 45.240000000000002] >>> #we can access the individual list elements, just as we access individual characters in strings >>> measurements[0] 45.270000000000003 >>> measurements[1] 45.259999999999998 >>> measurements[2] 45.240000000000002 >>> measurements[3] # what's going to happen when I enter this? An error! there are only 3 elements in the list Traceback (most recent call last): File "", line 1, in IndexError: list index out of range >>> # lists can contain any type of object, not just ints. for example, strings >>> instructors = ["jen","mary", "juan"] >>> instructors ['jen', 'mary', 'juan'] >>> # a list can contain different types of things, say a student record >>> # name, student number, grade average, address >>> student = ["John_Smith",3321456, 97.5, "132 Main St, Pittsburgh, PA 15260"] >>> student ['John_Smith', 3321456, 97.5, '132 Main St, Pittsburgh, PA 15260'] >>> student[0] 'John_Smith' >>> student[1] 3321456 >>> student[2] 97.5 >>> student[3] '132 Main St, Pittsburgh, PA 15260' >>> len(student) # student has 4 elements 4 >>> # list functions >>> len(mesaurements) Traceback (most recent call last): File "", line 1, in NameError: name 'mesaurements' is not defined >>> len(measurements) 3 >>> max(measurements) 45.270000000000003 >>> min(measurements) 45.240000000000002 >>> sum(measurements) 135.77000000000001 >>> # sum adds up all the elements in the list. It works only if all the list elements are numbers >>> # The following will cause an error, since it doesn't make sense to sum strings and numbers >>> sum(student) Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for +: 'int' and 'str' >>> # now, for list methods >>> #first, let's see everything defined for lists >>> dir(lists) Traceback (most recent call last): File "", line 1, in NameError: name 'lists' is not defined >>> dir(list) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__str__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] >>> # here are some really useful ones >>> help(list.append) Help on method_descriptor: append(...) L.append(object) -- append object to end >>> help(list.count) Help on method_descriptor: count(...) L.count(value) -> integer -- return number of occurrences of value >>> help(list.insert) Help on method_descriptor: insert(...) L.insert(index, object) -- insert object before index >>> help(list.find) Traceback (most recent call last): File "", line 1, in AttributeError: type object 'list' has no attribute 'find' >>> # Let's see some in action. You should also play with these yourself in the shell >>> measurements [45.270000000000003, 45.259999999999998, 45.240000000000002] >>> measurements.append(40.32) >>> measurements [45.270000000000003, 45.259999999999998, 45.240000000000002, 40.32] >>> #following sayd to insert a 33 at position 2 in the list >>> measurements.insert(2,33) >>> measurements [45.270000000000003, 45.259999999999998, 33, 45.240000000000002, 40.32] >>> # as you can see, Python is smart - It made room for 33 to be inserted so now it is at position 2 >>> #for loops and lists: >>> for datum in measurements: ... print datum ... 45.27 45.26 33 45.24 40.32 >>> # For the rest of the class, we developed the code in first_list_code.py, tracing some on the board >>> # and tracing some in Wing.