Iteration - PowerPoint PPT Presentation

1 / 17

Iteration. When statements are repeated, any simpler way ? A for statement with a range Tell Python that I have an iteration Tell Python how many times. def drawSquare ( myTurtle,sideLength ): myTurtle.forward ( sideLength ) myTurtle.right (90) # side 1 myTurtle.forward ( sideLength )

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Iteration

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Iteration

• When statements are repeated, any simpler way ?

• A for statement with a range

• Tell Python that I have an iteration

• Tell Python how many times

defdrawSquare(myTurtle,sideLength):

myTurtle.forward(sideLength)

myTurtle.right(90) # side 1

myTurtle.forward(sideLength)

myTurtle.right(90) # side 2

myTurtle.forward(sideLength)

myTurtle.right(90) # side 3

myTurtle.forward(sideLength)

myTurtle.right(90) # side 4

Listing 1.3

defdrawS(myT,sideLength):

for i in range(0,4):

myT.forward(sideLength)

myTurtle.right(90)

defdrawSquare(myTurtle,sideLength):

myTurtle.forward(sideLength)

myTurtle.right(90) # side 1

myTurtle.forward(sideLength)

myTurtle.right(90) # side 2

myTurtle.forward(sideLength)

myTurtle.right(90) # side 3

myTurtle.forward(sideLength)

myTurtle.right(90) # side 4

FOR loop

• “for i in range(0,4):”

• “i” – a dummy variable keeping counts

• range(a, b):

• a, a+1, a+2, ….. , b-2, b-1

• Do not forget “:” at the end

• Statements to be repeated have to be indented

Figure 1.12

Another example

• Five numbers to get an average: 20, 30, 40, 5, 55

• A general way of getting an average of any five numbers ?

• A collection of five numbers – how to tell what it is ?

>>> avg2 = (20+30+40+5+55)/5

Example

• Compute the class average

Initialize total to zero

Initialize counter to zero

while the user has not as yet entered the sentinel

input the next grade (possibly the sentinel)

if the counter is not equal to zero

set the average to the total divided by the counter

print the average

else

Example

• Compute the class average

total = 0

counter = 0

counter = counter+1

if counter > 0:

average = total/counter

print average

else:

Average

• List – an ordered set

• Index starting with 0 identifies each item in a list

• A general way of getting an average of any five numbers ?

• But, would be nice to ‘automate’ the summing process

• => Further abstraction

>>> myList = [20,30,40,5,55]

>>> avg1 = (myList[0]+myList[1]+myList[2]+myList[3]+myList[4])/5

Getting the sum

sum = myList[0]+myList[1]+myList[2]+myList[3]+myList[4]

• How to automate the sum ?

sum = 0

sum = sum + myList[0]

sum = sum + myList[1]

sum = sum + myList[2]

sum = sum + myList[3]

sum = sum + myList[4]

Getting the sum

sum = 0

sum = sum + myList[0]

sum = sum + myList[1]

sum = sum + myList[2]

sum = sum + myList[3]

sum = sum + myList[4]

• Haven’t really automate the process, but we have

sum = 0

sum = sum + myList[ ]

sum = sum + myList[ ]

sum = sum + myList[ ]

sum = sum + myList[ ]

sum = sum + myList[ ]

“for” loop

sum = 0

sum = sum + myList[0]

sum = sum + myList[1]

sum = sum + myList[2]

sum = sum + myList[3]

sum = sum + myList[4]

• “for” steps through every element

sum = 0

forin range(0, 5):

sum = sum + myList[ ]

• Use any variable name

“for” loop

for p in range(min, max):

block

p=min

true

p = max

false

Do block

p = p +1

Out of “for”

Program an average

• Remember getting an average ?

• Can you write a command (in ???? below) to compute the average using ‘psum’

>>> myList = [20,30,40,5,55]

>>> avg1 = (myList[0]+myList[1]+myList[2]+myList[3]+myList[4])/5

>>> myList = [-20, 50, -4, 20, 7]

>>> n=5

>>> psum = 0

>>> for i in range(0,n):

… psum = psum + myList[i]

>>> ????

sum Function

• Hate typing an array of values, “for i in range(,):” each time you compute a sum

• Any easier way ? -- YES !!

• Create a GENERIC function to compute a sum regardless of arrays of values

• => Further ABSTRACTION

sum Function

def sum(thisList):

n = len(thisList)

psum = 0

for j in range(0,n):

psum = psum+thisList[j]

return psum

Listing 1.4

def drawSpiral(myTurtle,maxSide):

for sideLength in range(1,maxSide+1,5):

myTurtle.forward(sideLength)

myTurtle.right(90)