eecs 110: lec 10: definite loops and user input

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

Upload: ray

Post on 12-Jan-2016

39 views

Category:

Documents


0 download

DESCRIPTION

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 ]. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 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/

Page 2: EECS 110: Lec 10:  Definite Loops and User Input

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?

Page 3: EECS 110: Lec 10:  Definite Loops and User Input

fore!

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

Page 4: EECS 110: Lec 10:  Definite Loops and User Input

four on for

for x in range(8):

print 'x is', x

factorial function?

sum the list?

construct the list?

Page 5: EECS 110: Lec 10:  Definite Loops and User Input

Fact with for

def fact( n ):

answer = 1

for x in range(n):

answer = answer * x

return answer

Page 6: EECS 110: Lec 10:  Definite Loops and User Input

Fact with for

def fact( n ):

answer = 1

for x in range(1,n+1):

answer = answer * x

return answer

Page 7: EECS 110: Lec 10:  Definite Loops and User Input

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?

Page 8: EECS 110: Lec 10:  Definite Loops and User Input

Shortcut

Shortcuts for changing variables:

age = 38

age = age + 1

age += 1 #shortcut for age = age + 1

Page 9: EECS 110: Lec 10:  Definite Loops and User Input

Two kinds of for loops

Element-based Loops

sum = 0

for x in L: sum += x

L = [ 42, -10, 4 ]

x

"selfless"

Page 10: EECS 110: Lec 10:  Definite Loops and User Input

Two kinds of for loops

Element-based Loops

L = [ 42, -10, 4 ]

x

L = [ 42, -10, 4 ]

i0 1 2

Index-based Loops

sum = 0

for x in L: sum += x

sum = 0

for i in : sum +=

Page 11: EECS 110: Lec 10:  Definite Loops and User Input

Two kinds of for loops

Element-based Loops

L = [ 42, -10, 4 ]

x

L = [ 42, -10, 4 ]

i0 1 2

Index-based Loops

sum = 0

for x in L: sum += x

sum = 0

for i in range(len(L)): sum += L[i]

L[i]

Page 12: EECS 110: Lec 10:  Definite Loops and User Input

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?

Page 13: EECS 110: Lec 10:  Definite Loops and User Input

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?

Page 14: EECS 110: Lec 10:  Definite Loops and User Input

Extreme Looping

What does this code do?

print 'It keeps on'

while True: print 'going and'

print 'Phew! I\'m done!'

Page 15: EECS 110: Lec 10:  Definite Loops and User Input

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!

Page 16: EECS 110: Lec 10:  Definite Loops and User Input

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

Page 17: EECS 110: Lec 10:  Definite Loops and User Input

Making our escape!

import randomescape = 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?

Page 18: EECS 110: Lec 10:  Definite Loops and User Input

Loops aren't just for lists…

for c in 'down with CS!':

print c

Page 19: EECS 110: Lec 10:  Definite Loops and User Input

"Quiz"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, LL is a list of numbers.

n = 3while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1

def isPrime( n ):

Names:

Write a loop so that this function returns True if its input is prime and False otherwise:

n is a positive integer

Page 20: EECS 110: Lec 10:  Definite Loops and User Input

What do these two loops print?

n = 0

for c in 'forty-two':

if c not in 'aeiou':

n += 1

print n

??

n = 3while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1

Page 21: EECS 110: Lec 10:  Definite Loops and User Input

What do these two loops print?

n = 0

for c in 'forty-two':

if c not in 'aeiou':

n += 1

print n

7

n = 3while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1

??

Page 22: EECS 110: Lec 10:  Definite Loops and User Input

What do these two loops print?

n = 0

for c in 'forty-two':

if c not in 'aeiou':

n += 1

print n

7

n = 3while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1

3

Page 23: EECS 110: Lec 10:  Definite Loops and User Input

What do these two loops print?

n = 0

for c in 'forty-two':

if c not in 'aeiou':

n += 1

print n

7

n = 3while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1

310

Page 24: EECS 110: Lec 10:  Definite Loops and User Input

What do these two loops print?

n = 0

for c in 'forty-two':

if c not in 'aeiou':

n += 1

print n

7

n = 3while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1

3105

Page 25: EECS 110: Lec 10:  Definite Loops and User Input

What do these two loops print?

n = 0

for c in 'forty-two':

if c not in 'aeiou':

n += 1

print n

7

n = 3while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1

310516

Page 26: EECS 110: Lec 10:  Definite Loops and User Input

What do these two loops print?

n = 0

for c in 'forty-two':

if c not in 'aeiou':

n += 1

print n

7

n = 3while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1

3105168

Page 27: EECS 110: Lec 10:  Definite Loops and User Input

What do these two loops print?

n = 0

for c in 'forty-two':

if c not in 'aeiou':

n += 1

print n

7

n = 3while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1

31051684

Page 28: EECS 110: Lec 10:  Definite Loops and User Input

What do these two loops print?

n = 0

for c in 'forty-two':

if c not in 'aeiou':

n += 1

print n

7

n = 3while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1

310516842

Page 29: EECS 110: Lec 10:  Definite Loops and User Input

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

Page 30: EECS 110: Lec 10:  Definite Loops and User Input

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

mn = L[0]

for i in range(1,len(L)):

if L[i] < mn:

mn = L[i]

return mn

Page 31: EECS 110: Lec 10:  Definite Loops and User Input

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

Page 32: EECS 110: Lec 10:  Definite Loops and User Input

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

Page 33: EECS 110: Lec 10:  Definite Loops and User Input

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.

Real axis

Imaginary axis

c

Lab 8: the Mandelbrot Set

Benoit M.

z0

z1z2

z3

z4

Page 34: EECS 110: Lec 10:  Definite Loops and User Input

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.

Real axis

Imaginary axis

c

Lab 8: the Mandelbrot Set

Benoit M.

z0

z1z2

z3

z4

example of a non-diverging cycle

Page 35: EECS 110: Lec 10:  Definite Loops and User Input

Consider the following update rule for all complex numbers c:

z0 = 0

zn+1 = zn2 + c

Lab 8: the Mandelbrot Set

The shaded area are points that do not diverge.

Page 36: EECS 110: Lec 10:  Definite Loops and User Input

Python and images

for creating and saving imagesimport bmp.py

image = BitMap( 300, 200, Color.GREEN )

creates a bitmap objectand names it image

Page 37: EECS 110: Lec 10:  Definite Loops and User Input

Python and images

for creating and saving imagesimport bmp.py

image = BitMap( 300, 200, Color.GREEN )

creates a bitmap objectand names it image

objects are software abstractions containing structured information

Page 38: EECS 110: Lec 10:  Definite Loops and User Input

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" )

Page 39: EECS 110: Lec 10:  Definite Loops and User Input

Python and images and objects

import bmp.py

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 )

Page 40: EECS 110: Lec 10:  Definite Loops and User Input

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" )

Page 41: EECS 110: Lec 10:  Definite Loops and User Input

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" )

Page 42: EECS 110: Lec 10:  Definite Loops and User Input

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" )

Page 43: EECS 110: Lec 10:  Definite Loops and User Input

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" )

Page 44: EECS 110: Lec 10:  Definite Loops and User Input

See you in Lab!