cs 61a lecture 10
play

CS 61A Lecture 10 Friday, February 13 Announcements 2 - PowerPoint PPT Presentation

CS 61A Lecture 10 Friday, February 13 Announcements 2 Announcements Guerrilla Section 2 is on Monday 2/16 2 Announcements Guerrilla Section 2 is on Monday 2/16 RSVP on Piazza if you want to come! 2 Announcements Guerrilla


  1. CS 61A Lecture 10 Friday, February 13

  2. Announcements 2

  3. Announcements • Guerrilla Section 2 is on Monday 2/16 2

  4. Announcements • Guerrilla Section 2 is on Monday 2/16 § RSVP on Piazza if you want to come! 2

  5. Announcements • Guerrilla Section 2 is on Monday 2/16 § RSVP on Piazza if you want to come! • Homework 3 due Thursday 2/19 @ 11:59pm (extended) 2

  6. Announcements • Guerrilla Section 2 is on Monday 2/16 § RSVP on Piazza if you want to come! • Homework 3 due Thursday 2/19 @ 11:59pm (extended) § Homework Party on Tuesday 2/17 5pm-6:30pm in 2050 VLSB 2

  7. Announcements • Guerrilla Section 2 is on Monday 2/16 § RSVP on Piazza if you want to come! • Homework 3 due Thursday 2/19 @ 11:59pm (extended) § Homework Party on Tuesday 2/17 5pm-6:30pm in 2050 VLSB • Optional Hog Contest due Wednesday 2/18 @ 11:59pm 2

  8. Sequences

  9. The Sequence Abstraction 4

  10. The Sequence Abstraction red, orange, yellow, green, blue, indigo, violet. 4

  11. The Sequence Abstraction red, orange, yellow, green, blue, indigo, violet. There isn't just one sequence class or data abstraction (in Python or in general). 4

  12. The Sequence Abstraction red, orange, yellow, green, blue, indigo, violet. There isn't just one sequence class or data abstraction (in Python or in general). The sequence abstraction is a collection of behaviors: 4

  13. The Sequence Abstraction red, orange, yellow, green, blue, indigo, violet. There isn't just one sequence class or data abstraction (in Python or in general). The sequence abstraction is a collection of behaviors: Length . A sequence has a finite length. Element selection . A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0. 4

  14. The Sequence Abstraction red, orange, yellow, green, blue, indigo, violet. 0 , 1 , 2 , 3 , 4 , 5 , 6 . There isn't just one sequence class or data abstraction (in Python or in general). The sequence abstraction is a collection of behaviors: Length . A sequence has a finite length. Element selection . A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0. 4

  15. The Sequence Abstraction red, orange, yellow, green, blue, indigo, violet. 0 , 1 , 2 , 3 , 4 , 5 , 6 . There isn't just one sequence class or data abstraction (in Python or in general). The sequence abstraction is a collection of behaviors: Length . A sequence has a finite length. Element selection . A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0. There is built-in syntax associated with this behavior, or we can use functions. 4

  16. The Sequence Abstraction red, orange, yellow, green, blue, indigo, violet. 0 , 1 , 2 , 3 , 4 , 5 , 6 . There isn't just one sequence class or data abstraction (in Python or in general). The sequence abstraction is a collection of behaviors: Length . A sequence has a finite length. Element selection . A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0. There is built-in syntax associated with this behavior, or we can use functions. A list is a kind of built-in sequence 4

  17. Lists ['Demo']

  18. Lists are Sequences 6

  19. Lists are Sequences >>> digits = [1, 8, 2, 8] 6

  20. Lists are Sequences >>> digits = [1, 8, 2, 8] >>> len(digits) 4 6

  21. Lists are Sequences >>> digits = [1, 8, 2, 8] >>> len(digits) 4 >>> digits[3] 8 6

  22. Lists are Sequences >>> digits = [1, 8, 2, 8] >>> len(digits) 4 >>> digits[3] 8 Length . A sequence has a finite length. Element selection . A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0. 6

  23. Lists are Sequences >>> digits = [1, 8, 2, 8] >>> len(digits) 4 >>> digits[3] 8 Length . A sequence has a finite length. Element selection . A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0. >>> [2, 7] + digits * 2 [2, 7, 1, 8, 2, 8, 1, 8, 2, 8] 6

  24. Lists are Sequences >>> digits = [1, 8, 2, 8] >>> len(digits) 4 >>> digits[3] 8 Length . A sequence has a finite length. Element selection . A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0. >>> [2, 7] + digits * 2 [2, 7, 1, 8, 2, 8, 1, 8, 2, 8] >>> pairs = [[10, 20], [30, 40]] 6

  25. Lists are Sequences >>> digits = [1, 8, 2, 8] >>> len(digits) 4 >>> digits[3] 8 Length . A sequence has a finite length. Element selection . A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0. >>> [2, 7] + digits * 2 [2, 7, 1, 8, 2, 8, 1, 8, 2, 8] >>> pairs = [[10, 20], [30, 40]] >>> pairs[1] [30, 40] 6

  26. Lists are Sequences >>> digits = [1, 8, 2, 8] >>> len(digits) 4 >>> digits[3] 8 Length . A sequence has a finite length. Element selection . A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0. >>> [2, 7] + digits * 2 [2, 7, 1, 8, 2, 8, 1, 8, 2, 8] >>> pairs = [[10, 20], [30, 40]] >>> pairs[1] [30, 40] >>> pairs[1][0] 30 6

  27. For Statements (Demo)

  28. Sequence Iteration 8

  29. Sequence Iteration def count(s, value): total = 0 for element in s: � � � � � if element == value: total = total + 1 return total 8

  30. Sequence Iteration def count(s, value): total = 0 for element in s: � Name bound in the first frame � of the current environment � (not a new frame) � � if element == value: total = total + 1 return total 8

  31. For Statement Execution Procedure 9

  32. For Statement Execution Procedure for <name> in <expression>: <suite> 9

  33. For Statement Execution Procedure for <name> in <expression>: <suite> 1. Evaluate the header <expression>, which must yield an iterable value (a sequence) 9

  34. For Statement Execution Procedure for <name> in <expression>: <suite> 1. Evaluate the header <expression>, which must yield an iterable value (a sequence) 2. For each element in that sequence, in order: 9

  35. For Statement Execution Procedure for <name> in <expression>: <suite> 1. Evaluate the header <expression>, which must yield an iterable value (a sequence) 2. For each element in that sequence, in order: A. Bind <name> to that element in the current frame 9

  36. For Statement Execution Procedure for <name> in <expression>: <suite> 1. Evaluate the header <expression>, which must yield an iterable value (a sequence) 2. For each element in that sequence, in order: A. Bind <name> to that element in the current frame B. Execute the <suite> 9

  37. Sequence Unpacking in For Statements 10

  38. Sequence Unpacking in For Statements >>> pairs = [[1, 2], [2, 2], [3, 2], [4, 4]] � >>> same_count = 0 10

  39. Sequence Unpacking in For Statements A sequence of 
 fixed-length sequences >>> pairs = [[1, 2], [2, 2], [3, 2], [4, 4]] � >>> same_count = 0 10

  40. Sequence Unpacking in For Statements A sequence of 
 fixed-length sequences >>> pairs = [[1, 2], [2, 2], [3, 2], [4, 4]] � >>> same_count = 0 >>> for x, y in pairs: ... if x == y: ... same_count = same_count + 1 � >>> same_count 2 10

  41. Sequence Unpacking in For Statements A sequence of 
 fixed-length sequences >>> pairs = [[1, 2], [2, 2], [3, 2], [4, 4]] � >>> same_count = 0 A name for each element in a fixed-length sequence >>> for x, y in pairs: ... if x == y: ... same_count = same_count + 1 � >>> same_count 2 10

  42. Sequence Unpacking in For Statements A sequence of 
 fixed-length sequences >>> pairs = [[1, 2], [2, 2], [3, 2], [4, 4]] � >>> same_count = 0 A name for each element in a Each name is bound to a value, as in fixed-length sequence multiple assignment >>> for x, y in pairs: ... if x == y: ... same_count = same_count + 1 � >>> same_count 2 10

  43. Ranges

  44. The Range Type A range is a sequence of consecutive integers. * 12

  45. The Range Type A range is a sequence of consecutive integers. * * Ranges can actually represent more general integer sequences. 12

  46. The Range Type A range is a sequence of consecutive integers. * ..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ... * Ranges can actually represent more general integer sequences. 12

  47. The Range Type A range is a sequence of consecutive integers. * ..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ... range(-2, 2) * Ranges can actually represent more general integer sequences. 12

  48. The Range Type A range is a sequence of consecutive integers. * ..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ... range(-2, 2) * Ranges can actually represent more general integer sequences. 12

Recommend


More recommend