Presentation is loading. Please wait.

Presentation is loading. Please wait.

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 ?

Similar presentations


Presentation on theme: "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 ?"— Presentation transcript:

1 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 ?

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

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

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

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

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

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

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

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

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

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

12 Sum every other element 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 Finding the sum of a list: Accumulator! shortcuts? vs. recursion? sum every OTHER element?

13 Extreme Looping What does this code do? print 'It keeps on' while True: print('going and’) print('Phew! I\'m done!’)

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

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

16 Making our escape! import random escape = 0 while escape != 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?

17 Loops aren't just for lists… for c in 'down with CS!': print ( c )

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

19 What do these two loops print? n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1 print(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 = 0 for c in 'forty-two': if c not in 'aeiou': n += 1 print n 7 n = 3 while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1 ??

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

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

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

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

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

26 What do these two loops print? n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1 print n 7 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

27 What do these two loops print? n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1 print n 7 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

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

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

30 L is a list of numbers. def isPrime( n ): n is a positive integer 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

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 ): for i in range (n): if i not in [0,1]: if n%i == 0: return False return True n is a positive integer

32 Consider the following update rule for all complex numbers c: z 0 = 0 z n+1 = z n 2 + c If z does not diverge, c is in the M. Set. Real axis Imaginary axis c the Mandelbrot Set Benoit M. z0z0 z1z1 z2z2 z3z3 z4z4

33 Consider the following update rule for all complex numbers c: z 0 = 0 z n+1 = z n 2 + c If c does not diverge, it's in the M. Set. Real axis Imaginary axis c the Mandelbrot Set Benoit M. z0z0 z1z1 z2z2 z3z3 z4z4 example of a non-diverging cycle

34 Consider the following update rule for all complex numbers c: z 0 = 0 z n+1 = z n 2 + c the Mandelbrot Set The shaded area are points that do not diverge.

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

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

37 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 objects are variables that can contain their own functions, often called methods image.saveFile( "test.bmp" )

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

39 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 in range(width): for row in range(height): if col == row: image.plotPoint( col, row ) image.saveFile( "test.bmp" )

40 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 in range(width): for row in range(height): if col == row: image.plotPoint( col, row ) image.saveFile( "test.bmp" )

41 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 in range(width): for row in range(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 in range(width): for row in range(height): if col == height – row -1: image.plotPoint( col, row ) image.saveFile( "test.bmp" )


Download ppt "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 ?"

Similar presentations


Ads by Google