Lecture outline
Goals
- Review self-paced introduction material
- Create and manipulate lists
- Learn more advanced flow control: loops, nested control structures
- Create and manipulate dicts
- Learn what a module is and how to access its functions
- Apply all these concepts to understanding a real program
Lecture outline
Review tutorial material
2+2 5-2 3 * 4 4 / 22*2 + 10*21 / 21.0 / 2type(1) type(1.0)x = 4 y = 2.5 x y"Hello" "Hello, world!" "Testing 1, 2, 3"x = "Hello" xtype(True) type(true) type(False) type(false)0 == 0 0 == 1 0 != 12 > 1 1 < 2 2 >= 5if 2 > 1: print "Two is greater than one."x = 5 y = 3 if y > x: print "y is greater than x." else: print: "x is greater than y."date = 28 if date > 26 and date < 31: print "Time for ALA Annual." else: print "Still time to pack!"date = 28 if date > 26 and date < 31: print "Time for ALA Annual." elif date == 28: print "Time for my preconference!" else: print "Still time to pack!"def multiply(x, y): result = x * y return resultmultiply(2, 3)
Lists
todo_list = ['initialize list', 'find its length', 'access elements'] todo_list len(todo_list)number_list = [3, 1, 4, 1, 5]todo_list[1]todo_list.append('add an element') todo_listtodo_list_two = ['coffee break', 'learn about negative indexing'] todo_list + todo_list_twotodo_list += todo_list_two todo_listtodo_list[:2]todo_list[2:]Loops and more flow control
pie = ["strawberry", "blueberry", "raspberry"]print pie[0] print pie[1] print pie [2]More information on RSIs at Wikipedia. In short, they stink. Remember to take frequent breaks while programming!It'll do the job, but do we really want to write the same code over and over again for each element? Do you like RSIs? I don't.
Enter loops. Loops allow you to work through lists efficiently by only having to write one piece of code for all (or a subset of) the elements of a list. There are different types of loops that you can use for different situations. For our example, we will use the
for
loop.And here it is! Do you know what's going on in the
for
loop below?for fruit in pie: print fruitfor fruit in pie: if fruit == "strawberry": print "I like strawberry!" else: print fruitapproved_berries = ["strawberry", "currants", "elderberry"]i = 0 for fruit in pie: for berry in approved_berries: if fruit == berry: i += 1 print str(i) + ' approved berries in this pie'range(14) range(3,14)for fruit in range(len(pie)): print pie[fruit]i = 0 infinite = True while infinite: i += 1 print ii = 0 infinite = True while infinite: i += 1 if i == 10: break else: print ianswer = "" while answer != "yes": answer = raw_input("Are you enjoying the workshop so far?") if answer == "yes": print "Glad to hear that!" else: print "You will enjoy it even if you aren't. Try again."Dictionaries
words = { 'python': 'an awesome programming language', 'anaconda': 'a terrifying snake' }>>> words[ 'python' ] 'an awesome programming language' >>> words[ 'anaconda' ] 'a terrifying snake'>>> words[ 'boa constrictor' ] = 'an even more terrifying snake' >>> words[ 'boa constrictor' ] 'an even more terrifying snake' >>> words[ 'anaconda' ] = 'a slightly less terrifying snake' >>> words[ 'anaconda' ] 'a slightly less terrifying snake'>>> words.keys() ['python', 'boa constrictor', 'anaconda'] >>> words.values() ['an awesome programming language', 'an even more terrifying snake', 'a slightly less terrifying snake']catalog = { 'last_updated': 'Sat Jun 15 17:32:53 EDT 2013', 'num_items': 213, 'items': { '9780596009250': { 'title': 'Programming Python', 'authors': [ 'Mark Lutz' ], 'pub_year': 2006 }, '9780596002817': { 'title': 'Learning Python', 'authors': [ 'Mark Lutz', 'David Ascher' ], 'pub_year': 2003 } }, 'location_codes': [ 'stacks', 'reserves', 'special collections' ] }Modules
Next Step:
On to Lightning talks!
Back to Checkoff