1 / 44

EECS 110: Lec 10: Definite Loops and User Input

EECS 110: Lec 10: Definite Loops and User Input. Aleksandar Kuzmanovic Northwestern University. http://cs.northwestern.edu/~akuzma/classes/EECS110-s09/. Loops !. We've seen variables change in-place before:. [ x*6 for x in range(8) ]. [ 0, 6, 12, 18, 24, 30, 36, 42 ].

ray
Download Presentation

EECS 110: Lec 10: Definite Loops and User Input

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EECS 110: Lec 10: Definite Loops and User Input Aleksandar Kuzmanovic Northwestern University http://cs.northwestern.edu/~akuzma/classes/EECS110-s09/

  2. Loops! We've seen variables change in-place before: [ x*6 for x in range(8) ] [ 0, 6, 12, 18, 24, 30, 36, 42 ] remember range?

  3. fore! x is assigned each value from this sequence 1 for x in range(8): print'x is', x print'Phew!' 3 LOOP back to step 1 for EACH value in the list the BODY or BLOCK of the for loop runs with that x 2 Code AFTER the loop will not run until the loop is finished. 4

  4. four on for for x in range(8): print'x is', x factorial function? sum the list? construct the list?

  5. Fact with for def fact( n ): answer = 1 for x in range(n): answer = answer * x return answer

  6. Fact with for def fact( n ): answer = 1 for x in range(1,n+1): answer = answer * x return answer

  7. Accumulating an answer… Finding the sum of a list: def sum( L ): """ returns the sum of L's elements """ sum = 0 for x in L: sum = sum + x return sum Accumulator! shortcuts? vs. recursion? sum every OTHER element?

  8. Shortcut Shortcuts for changing variables: age = 38 age = age + 1 age += 1 #shortcut for age = age + 1

  9. Two kinds of for loops Element-based Loops sum = 0 for x in L: sum += x L = [ 42, -10, 4 ] x "selfless"

  10. Two kinds of for loops Element-based Loops Index-based Loops sum = 0 for x in L: sum += x sum = 0 for i in : sum += i 1 2 0 L = [ 42, -10, 4 ] L = [ 42, -10, 4 ] x

  11. Two kinds of for loops Element-based Loops Index-based Loops sum = 0 for x in L: sum += x sum = 0 for i inrange(len(L)): sum += L[i] i 1 2 0 L = [ 42, -10, 4 ] L = [ 42, -10, 4 ] x L[i]

  12. Sum every other element Finding the sum of a list: def sum( L ): """ returns the sum of L's elements """ sum = 0 for i in range(len(L)): if ________: sum += L[i] return sum Accumulator! shortcuts? vs. recursion? sum every OTHER element?

  13. Sum every other element Finding the sum of a list: def sum( L ): """ returns the sum of L's elements """ sum = 0 for i in range(len(L)): if i%2 == 0: sum += L[i] return sum Accumulator! shortcuts? vs. recursion? sum every OTHER element?

  14. Extreme Looping What does this code do? print'It keeps on' whileTrue: print'going and' print'Phew! I\'m done!'

  15. Extreme Looping Anatomy of a while loop: the loop keeps on running as long as this test is True print'It keeps on' whileTrue: print'going and' print'Phew! I\'m done!' “while” loop This won't print until the while loop finishes - in this case, never! alternative tests?

  16. Extreme Looping Slowing things down… import time print'It keeps on' whileTrue: print'going and' time.sleep(1) print'Phew! I\'m done!' the loop keeps on running as long as this test is True “while” loop

  17. Making our escape! import random escape = 0 whileescape != 42: print'Help! Let me out!' escape = random.choice([41,42,43]) print'At last!' how could we count the number of loops we run?

  18. Loops aren't just for lists… for c in'down with CS!': print c

  19. Names: "Quiz" Write a loop to find and return the min of a list, L L is a list of numbers. def min( L ): What do these two loops print? n = 0 for c in'forty-two': if c not in'aeiou': n += 1 printn Write a loop so that this function returns True if its input is prime and False otherwise: n is a positive integer def isPrime( n ): n = 3 while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1

  20. What do these two loops print? n = 3 while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1 n = 0 for c in'forty-two': if c not in'aeiou': n += 1 printn ??

  21. What do these two loops print? n = 3 while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1 ?? n = 0 for c in'forty-two': if c not in'aeiou': n += 1 printn 7

  22. What do these two loops print? n = 3 while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1 3 n = 0 for c in'forty-two': if c not in'aeiou': n += 1 printn 7

  23. What do these two loops print? n = 3 while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1 3 10 n = 0 for c in'forty-two': if c not in'aeiou': n += 1 printn 7

  24. What do these two loops print? n = 3 while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1 3 10 5 n = 0 for c in'forty-two': if c not in'aeiou': n += 1 printn 7

  25. What do these two loops print? n = 3 while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1 3 10 5 16 n = 0 for c in'forty-two': if c not in'aeiou': n += 1 printn 7

  26. What do these two loops print? n = 3 while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1 3 10 5 16 8 n = 0 for c in'forty-two': if c not in'aeiou': n += 1 printn 7

  27. What do these two loops print? n = 3 while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1 3 10 5 16 8 4 n = 0 for c in'forty-two': if c not in'aeiou': n += 1 printn 7

  28. What do these two loops print? n = 3 while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1 3 10 5 16 8 4 2 n = 0 for c in'forty-two': if c not in'aeiou': n += 1 printn 7

  29. def min( L ): L is a list of numbers. def isPrime( n ): n is a positive integer

  30. def min( L ): mn = L[0] for i in range(1,len(L)): if L[i] < mn: mn = L[i] return mn L is a list of numbers. def isPrime( n ): n is a positive integer

  31. def min( L ): mn = L[0] for i in range(1,len(L)): if L[i] < mn: mn = L[i] return mn def min( L ): mn=L[0] for s in L: if s < mn: mn = s return mn L is a list of numbers. def isPrime( n ): n is a positive integer

  32. def min( L ): mn = L[0] for i in range(1,len(L)): if L[i] < mn: mn = L[i] return mn def min( L ): mn=L[0] for s in L: if s < mn: mn = s return mn L is a list of numbers. def isPrime( n ): for i in range (n): if i not in [0,1]: if n%i == 0: return False return True n is a positive integer

  33. Lab 8: the Mandelbrot Set Consider the following update rule for all complex numbers c: z0 = 0 zn+1 = zn2 + c If z does not diverge, c is in the M. Set. Benoit M. Imaginary axis z3 c z2 z1 z4 Real axis z0

  34. Lab 8: the Mandelbrot Set Consider the following update rule for all complex numbers c: z0 = 0 zn+1 = zn2 + c If c does not diverge, it's in the M. Set. Benoit M. Imaginary axis z3 c z2 z1 z4 z0 Real axis example of a non-diverging cycle

  35. Lab 8: the Mandelbrot Set Consider the following update rule for all complex numbers c: z0 = 0 zn+1 = zn2 + c The shaded area are points that do not diverge.

  36. Python and images import bmp.py for creating and saving images image = BitMap( 300, 200, Color.GREEN ) creates a bitmap object and names it image

  37. Python and images import bmp.py for creating and saving images image = BitMap( 300, 200, Color.GREEN ) creates a bitmap object and names it image objects are software abstractions containing structured information

  38. Python and images and objects import bmp.py image = BitMap( 300, 200, Color.GREEN ) here, a bitmap object named image is calling an internal method named saveFile image.saveFile( "test.bmp" ) objects are variables that can contain their own functions, often called methods

  39. Python and images and objects import bmp.py image = BitMap( 300, 200, Color.GREEN ) image.setPenColor( Color.Red ) two more internal methods image.plotPixel( 150, 100 ) image.saveFile( "test.bmp" ) objects are variables that can contain their own functions, often called methods

  40. Q: What does this plot? from bmp import * def test(): """ image demonstration """ width = 200 height = 200 image = BitMap( width, height ) # a 2d loop over each pixel for col inrange(width): for row inrange(height): if col == row: image.plotPoint( col, row ) image.saveFile( "test.bmp" )

  41. Q: What does this plot? A: A diagonal in the SW -> NE direction from bmp import * def test(): """ image demonstration """ width = 200 height = 200 image = BitMap( width, height ) # a 2d loop over each pixel for col inrange(width): for row inrange(height): if col == row: image.plotPoint( col, row ) image.saveFile( "test.bmp" )

  42. How could you change this code so that it plots a diagonal from NW to SE? def test(): """ demonstrating images """ width = 200 height = 200 image = BitMap( width, height ) # a 2d loop over each pixel for col inrange(width): for row inrange(height): if col == row: image.plotPoint( col, row ) image.saveFile( "test.bmp" )

  43. How could you change this code so that it plots a diagonal from NW to SE? def test(): """ demonstrating images """ width = 200 height = 200 image = BitMap( width, height ) # a 2d loop over each pixel for col inrange(width): for row inrange(height): ifcol == height – row -1: image.plotPoint( col, row ) image.saveFile( "test.bmp" )

  44. See you in Lab!

More Related