lists sequences announcements for today
play

Lists (& Sequences) Announcements for Today (Optional) Reading - PowerPoint PPT Presentation

Lecture 12 Lists (& Sequences) Announcements for Today (Optional) Reading Assignments Read 10.0-10.2, 10.4-10.6 A2 is now graded Read all of Chapter 8 for Thu Access it in Gradescope Graded out of 50 points Prelim,


  1. Lecture 12 Lists (& Sequences)

  2. Announcements for Today (Optional) Reading Assignments • Read 10.0-10.2, 10.4-10.6 • A2 is now graded • Read all of Chapter 8 for Thu § Access it in Gradescope § Graded out of 50 points • Prelim, 10/17 at 7:30 pm § Mean : 43.9, Median : 47 § Material up to TODAY § A : 46 (58%), B : 37 (29%) § Study guide is posted § Times/rooms by last name • A3 due this Friday • Conflict with Prelim time? § Thurs last day for help § Submit conflict to CMS § Will grade over break § Applies to SDS students too 10/2/18 Lists & Sequences 2

  3. Sequences: Lists of Values String List • s = 'abc d' • x = [5, 6, 5, 9, 15, 23] 0 1 2 3 4 0 1 2 3 4 5 a b c d 5 6 5 9 15 23 • Put characters in quotes • Put values inside [ ] § Use \' for quote character § Separate by commas • Access characters with [] • Access values with [] § s[0] is 'a' § x[0] is 5 § s[5] causes an error § x[6] causes an error § x[0:2] is [5, 6] (excludes 2 nd 5) § s[0:2] is 'ab' (excludes c ) § s[2:] is 'c d' § x[3:] is [9, 15, 23] 10/2/18 Lists & Sequences 3

  4. Sequences: Lists of Values String List • s = 'abc d' • x = [5, 6, 5, 9, 15, 23] 0 1 2 3 4 0 1 2 3 4 5 a b c d 5 6 5 9 15 23 • Put characters in quotes • Put values inside [ ] § Use \' for quote character § Separate by commas Sequence is name given to both • Access characters with [] • Access values with [] § s[0] is 'a' § x[0] is 5 § s[5] causes an error § x[6] causes an error § x[0:2] is [5, 6] (excludes 2 nd 5) § s[0:2] is 'ab' (excludes c ) § s[2:] is 'c d' § x[3:] is [9, 15, 23] 10/2/18 Lists & Sequences 4

  5. Lists Have Methods Similar to String x = [5, 6, 5, 9, 15, 23] But you get length of • index(value) a list with a regular § Return position of the value function, not method: len(x) § ERROR if value is not there § x.index(9) evaluates to 3 • count(value) § Returns number of times value appears in list § x.count(5) evaluates to 2 10/2/18 Lists & Sequences 5

  6. Representing Lists Wrong Correct x x 5, 6, 7, -2 id1 Unique tab identifier Variable id1 holds id 0 5 Box is “too small” 1 7 to hold the list 2 4 Put list in 3 -2 a “folder” x = [5, 7, 4,-2] 10/2/18 Lists & Sequences 6

  7. Lists vs. Class Objects List RGB • Attributes are indexed • Attributes are named § Example: x[2] § Example: c.red c id3 x id2 id2 id3 list RGB 0 5 red 128 1 7 green 64 2 4 blue 255 3 -2 10/2/18 Lists & Sequences 7

  8. When Do We Need to Draw a Folder? • When the value contains other values § This is essentially want we mean by ‘object’ • When the value is mutable Type Container? Mutable? int No No float No No str Yes* No Point3 Yes Yes RGB Yes Yes list Yes Yes 10/2/18 Lists & Sequences 8

  9. Lists are Mutable • x = [5, 7,4,-2] • List assignment : 0 1 2 3 <var>[<index>] = <value> 5 7 4 -2 § Reassign at index § Affects folder contents • x[1] = 8 § Variable is unchanged id1 • Strings cannot do this 0 5 § s = 'Hello World!' 1 7 x id1 2 § s[0] = 'J' ERROR 4 3 -2 § String are immutable 10/2/18 Lists & Sequences 9

  10. Lists are Mutable • x = [5, 7,4,-2] • List assignment : 0 1 2 3 <var>[<index>] = <value> x 5 7 4 -2 § Reassign at index 8 § Affects folder contents • x[1] = 8 § Variable is unchanged id1 • Strings cannot do this 0 5 x 8 § s = 'Hello World!' 1 7 x id1 2 § s[0] = 'J' ERROR 4 3 -2 § String are immutable 10/2/18 Lists & Sequences 10

  11. Slice Assignment • Can embed a new list inside of a list § Syntax: <var>[<start>:<end>] = <list> § Replaces that range with content of list • Example: >>> a = [1,2,3] >>> b = [4,5] Replaces [1,2] >>> a[:2] = b with [4,5] >>> a [4, 5, 3]

  12. Lists Share Methods with Strings x = [5, 6, 5, 9, 15, 23] • index(value) These are § Return position of the value immutable § ERROR if value is not there methods § x.index(9) evaluates to 3 • count(value) § Returns number of times value appears in list § x.count(5) evaluates to 2

  13. List Methods Can Alter the List x = [5, 6, 5, 9] • append(value) § A procedure method , not a fruitful method § Adds a new value to the end of list § x.append(-1) changes the list to [5, 6, 5, 9, -1] • insert(index, value) § Put the value into list at index; shift rest of list right § x.insert(2,-1) changes the list to [5, 6, -1, 5, 9,] • sort() 10/2/18 Lists & Sequences 13

  14. List Methods Can Alter the List x = [5, 6, 5, 9] • append(value) § A procedure method , not a fruitful method § Adds a new value to the end of list § x.append(-1) changes the list to [5, 6, 5, 9, -1] • insert(index, value) § Put the value into list at index; shift rest of list right § x.insert(2,-1) changes the list to [5, 6, -1, 5, 9,] • sort() What do you think this does? 10/2/18 Lists & Sequences 14

  15. Where To Learn About List Methods? In the documentation!

  16. Lists and Functions: Swap 1. def swap(b, h, k): Swaps b[h] and b[k], 2. """ Swaps b[h] and b[k] in b because parameter b Precond : b is a mutable list, 3. contains name of list. 4. h, k are valid positions""" 5. temp= b[h] id4 6. b[h]= b[k] 0 5 swap 5 7. b[k]= temp 1 4 b id4 h 3 2 7 3 6 swap(x, 3, 4) k 4 4 5 x id4 10/2/18 Lists & Sequences 16

  17. Lists and Functions: Swap 1. def swap(b, h, k): Swaps b[h] and b[k], 2. """ Swaps b[h] and b[k] in b because parameter b Precond : b is a mutable list, 3. contains name of list. 4. h, k are valid positions""" 5. temp= b[h] id4 6. b[h]= b[k] 0 5 swap 6 7. b[k]= temp 1 4 b id4 h 3 2 7 3 6 swap(x, 3, 4) temp 6 k 4 4 5 x id4 10/2/18 Lists & Sequences 17

  18. Lists and Functions: Swap 1. def swap(b, h, k): Swaps b[h] and b[k], 2. """ Swaps b[h] and b[k] in b because parameter b Precond : b is a mutable list, 3. contains name of list. 4. h, k are valid positions""" 5. temp= b[h] id4 6. b[h]= b[k] 0 5 swap 7 7. b[k]= temp 1 4 b id4 h 3 2 7 ✗ 3 5 6 swap(x, 3, 4) temp 6 k 4 4 5 x id4 10/2/18 Lists & Sequences 18

  19. Lists and Functions: Swap 1. def swap(b, h, k): Swaps b[h] and b[k], 2. """ Swaps b[h] and b[k] in b because parameter b Precond : b is a mutable list, 3. contains name of list. 4. h, k are valid positions""" 5. temp= b[h] id4 6. b[h]= b[k] 0 5 swap 7. b[k]= temp 1 4 b id4 h 3 2 7 ✗ 3 5 6 swap(x, 3, 4) temp 6 k 4 ✗ 4 6 5 x id4 10/2/18 Lists & Sequences 19

  20. List Slices Make Copies x = [5, 6, 5, 9] y = x [1:3] x y id5 id6 id5 id6 list list 0 6 0 5 1 5 1 6 2 5 3 9 copy = new folder 10/2/18 Lists & Sequences 20

  21. Exercise Time • Execute the following: >>> x = [5, 6, 5, 9, 10] >>> x[3] = -1 >>> x.insert(1,2) • What is x[4]? A: 10 B: 9 C: -1 D: ERROR E: I don’t know 10/2/18 Lists & Sequences 21

  22. Exercise Time • Execute the following: • Execute the following: >>> x = [5, 6, 5, 9, 10] >>> x = [5, 6, 5, 9, 10] >>> x[3] = -1 >>> y = x[1:] >>> x.insert(1,2) >>> y[0] = 7 • What is x[4]? • What is x[1]? A: 7 -1 B: 5 C: 6 D: ERROR E: I don’t know 10/2/18 Lists & Sequences 22

  23. Exercise Time • Execute the following: • Execute the following: >>> x = [5, 6, 5, 9, 10] >>> x = [5, 6, 5, 9, 10] >>> x[3] = -1 >>> y = x[1:] >>> x.insert(1,2) >>> y[0] = 7 • What is x[4]? • What is x[1]? -1 6 10/2/18 Lists & Sequences 23

  24. Lists and Expressions • List brackets [] can • Execute the following: contain expressions >>> a = 5 >>> b = 7 • This is a list expression >>> x = [a, b, a+b] § Python must evaluate it § Evaluates each expression • What is x[2]? § Puts the value in the list A: 'a+b' • Example: B: 12 >>> a = [1+2,3+4,5+6] C: 57 >>> a D: ERROR [3, 7, 11] E: I don’t know 10/2/18 Lists & Sequences 24

  25. Lists and Expressions • List brackets [] can • Execute the following: contain expressions >>> a = 5 >>> b = 7 • This is a list expression >>> x = [a, b, a+b] § Python must evaluate it § Evaluates each expression • What is x[2]? § Puts the value in the list • Example: 12 >>> a = [1+2,3+4,5+6] >>> a [3, 7, 11] 10/2/18 Lists & Sequences 25

  26. Lists of Objects • List positions are variables id13 r id10 § Can store base types list b id11 § But cannot store folders 0 id10 § Can store folder identifiers g id12 1 id11 • Folders linking to folders 2 x id13 id12 § Top folder for the list id12 § Other folders for contents id10 id11 RGB RGB • Example: RGB red 0 red 255 >>> r = introcs.RGB(255,0,0) red 0 green 0 green 0 >>> g = introcs.RGB(0,255,0) green 255 >>> b = introcs.RGB(0,0,255) blue 255 blue 0 blue 0 >>> x = [r,g,b] 10/2/18 Lists & Sequences 26

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend