% class-5 talking points Today - lists and tuples really important stuff, we must supplement the textbook more realistic (motivated) examples, more Python constructs (list comprehension) Review quiz Slice notation [0] first, [-1] last [3:6] lower:upper is inclusive:exclusive, indices 3,4,5. upper - lower is length of slice [:3] means [0:3], first 3 elements, not [3] [-3:] means from -3 to end, not the same as [-3:-1], which is exclusive for c in s:... index c binds to chars in s (not numbers) for line in file:... ditto, in general index var. binds to elts in collection after loop exits, index variable remains bound to last item loop blocks are not scopes like functions, ditto if... and other control blocks loop index variable remains in scope in enclosing blocks after loop exits block can't be empty, pass is "do nothing" statement. ----- ... this doesn't fit theme... printing strings - quotes don't appear in printed output print '%s %s' % (x,y) - format operator can be used anywhere, not just print must write \n in file to make line break ------ All of this applies to lists and tuples also! Lists, tuples, strings are all sequences. Lists and tuples Like strings, lists and tuples are sequences and iterables sequence: + and * operators, indices, slices iterable: for x in s:... unlike strings, lists and tuples can have any type in each element lists and tuples can be nested lists are mutable all list methods return None and update the list append vs. extend vs. + aliasing, "sharing", == vs. is, list arguments to functions (p. 111 - 115) traffic.py Use list of strings to model line of vehicles - functions for arrive, depart, notice we can update list from within functions. In main simulator, note use of arrive and depart function objects - you can do other things with functions besides calls them - you can put them in tuples, you can assign them to variables, test equality, look up their names. review __name__ == '__main__' convention review default args run it with several values of n tuples are immutable - like strings. type conversions list(...), tuple(...) nested.py Nested data structures are like web page DOM or XML file Use recursion to travserse, process nested data structure Nested data structures are self-similar, suggests recursion is best strategy for...: is better for flat data structures (with no nesting). route.py Use tuple of pairs (also tuples) to model a graph, one pair for each edge draw on board - it's a *directed* graph, edges point one way, draw arrows check path by checking that successive pairs of nodes are in graph note *graph parameter gathers however many arguments into a tuple (12.4,136) note path[i:i+2] is slice within path Quiz Break Discuss Quiz Presenters: Peter Conerly, Jon Crump, Jonathan Poland, Daniel Nguyen Break List comprehensions Create a new list from an existing list (or other iterable) [ pattern for element in source ] one element in new list for each element in source (or fewer if filter) phones.py list of tuples is like a database comprehension expression is like a database query physics_dept returns True or False depending on whether phone number in range tuple assignment: (name, phone) in phones physics_dept(phone) is an expression traverse.py find path through graph list comprehensions here are just scratch data. we *don't* use a comprehension for the path itself - because of dependencies *between* elements. run it several times - why do we see so few different results? - it's a *directed* graph, no way back If there's time create puget2 with reverse paths How do you *search* a graph? Hint: it's self-similar ...