- 107 Views
- Uploaded on
- Presentation posted in: General

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

- 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

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

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

Input the first grade

while the user has not as yet entered the sentinel

add this grade into the running total

add one to the grade counter

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

print 'no grades were entered'

Example

- Compute the class average

total = 0

counter = 0

while grades[counter] >=0:

total = total + grades[counter]

counter = counter+1

if counter > 0:

average = total/counter

print average

else:

print 'no grades were entered'

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

- How about ?
- 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

def drawSpiral(myTurtle,maxSide):

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

myTurtle.forward(sideLength)

myTurtle.right(90)