This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Module 8 Iteration, dictionaries and classes Where Python is very different from Scheme CS 116: Introduction to Computer Science 2 Daniel G. Brown/Troy Vasiga, University of Waterloo 8.1 Purpose of Module 8 • Iterative structure in Python • Python’s approach to dictionaries • Classes: storing information in objects • Readings: 7, 11, 15, 16 8.2 1 Iterative structure in Python Counting down How would we count down from 10 to 0 in Scheme? Let’s make the list that goes from a parameter x to 0. In Scheme, we do this with recursion: ;; count-down: nat → (listof nat) ( define ( count-down x ) ( cond [( zero ? x ) ( list 0) ] [ else ( cons x ( count-down ( sub1 x )))])) ( count-down 5) ⇒ ( list 5 4 3 2 1 0) Here, we’re using recursion, and counting our way from x down to 0. • The base case is when we’ve reached 0. • The inductive case places x at the beginning of the list. 8.3 Recursion in Python We could write that in Python: def count_down ( x ): if x == 0: return  else : return [ x ] + count_down ( x-1) Here, we use the addition operator: [ x ] + count_down ( x-1) means to join together the one-element list with x in it to the inductive case for x-1 . But that’s not how we’d normally expect to do something like that. Python doesn’t expect us to use recursion: instead, we use iteration . 8.4 1 1.1 Guarded iteration: while loops Iteration in Python We’ll use new keywords that we didn’t see in Scheme to take the place of recursion. These keywords take advantage of mutation, and mutate the variable that would be the inductive parameter in the recursive presentation. We use a while loop: def count_down ( x ): ## line 1 return_value = ## line 2 while x >= 0: ## line 3 return_value . append ( x ) ## line 4 x = x- 1 ## line 5 return return_value ## line 6 8.5 How does iteration work? If we execute: count_down (3) • We initialize return_value to the list , which is the empty list (at line 2). • We test if x >= 0 (at line 3). – The current value of the variable x is 3, so x >= 0 evaluates to True . • Since the test evaluates to True , we execute what is sitting inside the indented block below the while statement. – First, we append x to the end of the list for return_value . So it changes value from to  (at line 4). – Then we mutate x : we evaluate the expression x-1 (and get 2), and change the value of x from 3 to 2 (at line 5). • We finished the content indented under the while statement – We loop back to the test , to see if we’re done executing the while statement (at line 3). 8.6 Next two rounds of the iteration • Now, x equals 2, so x >= 0 is still True . – We add the value of x to the end of the return_value list, by calling the append method of return_value ....
View Full Document
This note was uploaded on 07/26/2011 for the course CS 116 taught by Professor T.vasiga during the Spring '09 term at Waterloo.
- Spring '09
- Computer Science