module8-print

# module8-print - Module 8 Iteration dictionaries and classes...

This preview shows pages 1–3. Sign up to view the full content.

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 [0] 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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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 [3] (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 . The value of return_value is now [3,2] . And we reduce x to 1. And we loop back to the test again. The value of x >= 0 is still True . So we execute the two statements again: now return_value is [3,2,1] and x is set to 0.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern