for loops announcements for this lecture reading
play

For-Loops Announcements for This Lecture Reading Assignments/Lab - PowerPoint PPT Presentation

Lecture 13 For-Loops Announcements for This Lecture Reading Assignments/Lab Today: Chapters 8, 10 A2 has been graded Pick up in Gates 216 Thursday: Chapter 11 Grades generally good Prelim, Oct 12 th 7:30-9:00 A3 is


  1. Lecture 13 For-Loops

  2. Announcements for This Lecture Reading Assignments/Lab • Today: Chapters 8, 10 • A2 has been graded § Pick up in Gates 216 • Thursday: Chapter 11 § Grades generally good • Prelim, Oct 12 th 7:30-9:00 • A3 is due on Thursday § Material up to TODAY § Will post survey today § Study guide is posted § Survey due next week • Review next Wednesday • Lab is on lists/for-loops § Room/Time are TBA § Due in two weeks § Will cover what is on exam § But fair game on exam 10/3/17 For Loops 2

  3. Example: Summing the Elements of a List def sum(thelist): """Returns: the sum of all elements in thelist Precondition: thelist is a list of all numbers (either floats or ints)""" pass # Stub to be implemented Remember our approach: Outline first; then implement 10/3/17 For Loops 3

  4. Example: Summing the Elements of a List def sum(thelist): """Returns: the sum of all elements in thelist Precondition: thelist is a list of all numbers (either floats or ints)""" # Create a variable to hold result (start at 0) # Add each list element to variable # Return the variable 10/3/17 For Loops 4

  5. Example: Summing the Elements of a List def sum(thelist): """Returns: the sum of all elements in thelist Precondition: thelist is a list of all numbers (either floats or ints)""" result = 0 result = result + thelist[0] result = result + thelist[1] … There is a return result problem here 10/3/17 For Loops 5

  6. Working with Sequences • Sequences are potentially unbounded § Number of elements inside them is not fixed § Functions must handle sequences of different lengths § Example : sum([1,2,3]) vs. sum([4,5,6,7,8,9,10]) • Cannot process with fixed number of lines § Each line of code can handle at most one element § What if # of elements > # of lines of code? • We need a new control structure 10/3/17 For Loops 6

  7. For Loops: Processing Sequences # Print contents of seq The for-loop: x = seq[0] for x in seq: print(x) print(x) x = seq[1] print(x) … • Key Concepts x = seq[len(seq)-1] § loop sequence: seq print(x) § loop variable : x § body : print(x) • Remember : § Cannot program … § Also called repetend 10/3/17 For Loops 7

  8. For Loops: Processing Sequences • loop sequence: seq The for-loop: • loop variable : x for x in seq: • body : print(x) print(x) To execute the for-loop: 1. Check if there is a “next” element of loop sequence True seq has 2. If not, terminate execution put next more elts elt in x 3. Otherwise, put the element in the loop variable 4. Execute all of the body False print(x) 5. Repeat as long as 1 is true 10/3/17 For Loops 8

  9. Example: Summing the Elements of a List def sum(thelist): """Returns: the sum of all elements in thelist Precondition: thelist is a list of all numbers (either floats or ints)""" # Create a variable to hold result (start at 0) # Add each list element to variable # Return the variable 10/3/17 For Loops 9

  10. Example: Summing the Elements of a List def sum(thelist): """Returns: the sum of all elements in thelist Precondition: thelist is a list of all numbers (either floats or ints)""" result = 0 • loop sequence: thelist for x in thelist: result = result + x • loop variable : x • body : result=result+x return result 10/3/17 For Loops 10

  11. Example: Summing the Elements of a List def sum(thelist): """Returns: the sum of all elements in thelist Precondition: thelist is a list of all numbers (either floats or ints)""" Accumulator result = 0 variable • loop sequence: thelist for x in thelist: result = result + x • loop variable : x • body : result=result+x return result 10/3/17 For Loops 11

  12. For Loops and Conditionals def num_ints(thelist): """Returns: the number of ints in thelist Precondition: thelist is a list of any mix of types""" # Create a variable to hold result (start at 0) # for each element in the list… # check if it is an int # add 1 if it is # Return the variable 10/3/17 For Loops 12

  13. For Loops and Conditionals def num_ints(thelist): """Returns: the number of ints in thelist Precondition: thelist is a list of any mix of types""" result = 0 for x in thelist: if type(x) == int: Body result = result+1 return result 10/3/17 For Loops 13

  14. Modifying the Contents of a List def add_one(thelist): """(Procedure) Adds 1 to every element in the list Precondition: thelist is a list of all numbers (either floats or ints)""" for x in thelist: DOES NOT WORK! x = x+1 # procedure; no return 10/3/17 For Loops 14

  15. For Loops and Call Frames def add_one(thelist): add_one(seq): """Adds 1 to every elt add_one 1 Pre: thelist is all numb.""" for x in thelist: 1 thelist id4 x = x+1 2 id4 seq id4 0 5 1 4 2 7 10/3/17 For Loops 15

  16. For Loops and Call Frames def add_one(thelist): add_one(seq): """Adds 1 to every elt add_one 2 Pre: thelist is all numb.""" for x in thelist: 1 thelist id4 x = x+1 2 x 5 id4 seq id4 0 5 1 4 2 7 10/3/17 For Loops 16

  17. For Loops and Call Frames Loop back def add_one(thelist): add_one(seq): to line 1 """Adds 1 to every elt add_one 1 Pre: thelist is all numb.""" for x in thelist: 1 thelist id4 x = x+1 2 x 6 id4 seq id4 Increments x in frame 0 5 Does not affect folder 1 4 2 7 10/3/17 For Loops 17

  18. For Loops and Call Frames def add_one(thelist): add_one(seq): """Adds 1 to every elt add_one 2 Pre: thelist is all numb.""" for x in thelist: 1 thelist id4 x = x+1 2 x 4 id4 seq id4 Next element stored in x. 0 5 Previous calculation lost. 1 4 2 7 10/3/17 For Loops 18

  19. For Loops and Call Frames Loop back def add_one(thelist): add_one(seq): to line 1 """Adds 1 to every elt add_one 1 Pre: thelist is all numb.""" for x in thelist: 1 thelist id4 x = x+1 2 x 5 id4 seq id4 0 5 1 4 2 7 10/3/17 For Loops 19

  20. For Loops and Call Frames def add_one(thelist): add_one(seq): """Adds 1 to every elt add_one 2 Pre: thelist is all numb.""" for x in thelist: 1 thelist id4 x = x+1 2 x 7 id4 seq id4 Next element stored in x. 0 5 Previous calculation lost. 1 4 2 7 10/3/17 For Loops 20

  21. For Loops and Call Frames Loop back def add_one(thelist): add_one(seq): to line 1 """Adds 1 to every elt add_one 1 Pre: thelist is all numb.""" for x in thelist: 1 thelist id4 x = x+1 2 x 8 id4 seq id4 0 5 1 4 2 7 10/3/17 For Loops 21

  22. For Loops and Call Frames def add_one(thelist): add_one(seq): """Adds 1 to every elt add_one Pre: thelist is all numb.""" for x in thelist: 1 thelist id4 x = x+1 2 x 8 id4 seq id4 Loop is completed. 0 5 Nothing new put in x. 1 4 2 7 10/3/17 For Loops 22

  23. For Loops and Call Frames def add_one(thelist): add_one(seq): """Adds 1 to every elt Pre: thelist is all numb.""" for x in thelist: 1 x = x+1 2 id4 seq id4 No changes 0 5 1 4 to folder 2 7 10/3/17 For Loops 23

  24. On The Other Hand def copy_add_one(thelist): """Returns: copy with 1 added to every element Precondition: thelist is a list of all numbers (either floats or ints)""" mycopy = [] # accumulator for x in thelist: Accumulator keeps x = x+1 result from being lost mycopy.append(x) # add to end of accumulator return mycopy 10/3/17 For Loops 24

  25. How Can We Modify A List? • Never modify loop var! • Need a second sequence • This is an infinite loop: • How about the positions ? for x in thelist: thelist = [5, 2, 7, 1] thelist.append(1) thepos = [0, 1, 2, 3] for x in thepos: Try this in Python Tutor to see what happens thelist[x] = thelist[x]+1 10/3/17 For Loops 25

  26. How Can We Modify A List? • Never modify loop var! • Need a second sequence • This is an infinite loop: • How about the positions ? for x in thelist: thelist = [5, 2, 7, 1] thelist.append(1) thepos = [0, 1, 2, 3] for x in thepos: Try this in Python Tutor to see what happens thelist[x] = thelist[x]+1 10/3/17 For Loops 26

  27. This is the Motivation for Iterators • Iterators are objects id1 § Contain data like a list seq id1 0 5 § But cannot slice them 1 4 • Access data with next() 2 7 § Function to get next value § Keeps going until end id2 alt id2 § Get an error if go to far ? • Can convert back & forth § myiter = iter(mylist) Type/Class § mylist = list(myiter) conversion 10/3/17 For Loops 27

  28. Iterators and Lists >>> seq = [5, 4, 7] id1 >>> alt = iter(seq) seq id1 0 5 >>> next(alt) 1 4 5 2 7 >>> next(alt) 4 id2 alt id2 >>> next(alt) ? 7 >>> next(alt) Traceback… 10/3/17 For Loops 28

  29. Iterators and For Loops >>> seq = [5, 4, 7] id1 >>> alt = iter(seq) seq id1 0 5 >>> for x in alt: 1 4 print(x) 2 7 5 Just like looping 4 over the list id2 alt id2 7 ? 10/3/17 For Loops 29

Recommend


More recommend