Docsity

Understanding Sequences: Lists and Tuples in Python, Slides of Introduction to Computing

Toggle sidebar
bg1
Lists: Sequences of Objects!
String!
• s = 'abc de'!
• Put characters in quotes!
§ Use \' for quote character!
• Access characters with [ ]!
§ s[0] is 'a'!
§ s[6] causes an error!
§ s[0:2] is 'ab' (excludes c)!
§ s[2:] is 'c de'!
List!
• x = [5, 6, 5, 9, 15, 23]!
• Put items inside [ ] !
§ Separate by commas!
• Access items with [ ]!
§ x[0] is 5!
§ x[6] causes an error!
§ x[0:2] is [5, 6] (excludes 2nd 5)!
§ x[3:] is [9, 15, 23] !
a!b!c! !d!
0 1 2 3 4
e!
5
5!6!8!9!15!
0 1 2 3 4
23 !
5
Sequence is a name we give to both!
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Understanding Sequences: Lists and Tuples in Python and more Slides Introduction to Computing in PDF only on Docsity!

Lists: Sequences of Objects

String

  • s = 'abc de'
  • Put characters in quotes § Use ' for quote character
  • Access characters with [ ] § s[0] is 'a' § s[6] causes an error § s[0:2] is 'ab' (excludes c) § s[2:] is 'c de'

List

  • x = [5, 6, 5, 9, 15, 23]
  • Put items inside [ ] § Separate by commas
  • Access items with [ ] § x[0] is 5 § x[6] causes an error § x[0:2] is [5, 6] (excludes 2 nd 5) § x[3:] is [9, 15, 23] a b c d 0 1 2 3 4 e 5 5 6 8 9 15 0 1 2 3 4 23 5 Sequence is a name we give to both

Things that Work for All Sequences

s = ‘slithy’ x = [5, 6, 9, 6, 15, 5]

x.index(5) → 0 x.count(6) → 2 len(x) → 6 x[4] → 15 x[1:3] → [6, 9] x[3:] → [6, 15, 5] x[–2] → 15 x + [1, 2] → [5, 6, 9, 6, 15, 5, 1, 2] x * 2 → [5, 6, 9, 6, 15, 5, 5, 6, 9, 6, 15, 5] 15 in x → True s.index(‘s’) → 0 s.count(‘t’) → 1 len(s) → 6 s[4] → “h” s[1:3] → “li” s[3:] → “thy” s[–2] → “h” s + ‘ toves’ → “slithy toves” s * 2 → “slithyslithy” ‘t’ in s → True the smallest i for which x[i] == 5 the number of i s for which x[i] == 6 methods built-in fn. slicing operators

Difference: Lists are mutable

  • Their contents can be altered § by assignment to list items x = [5, 7, 3, 1] x[1] = 8 § using methods x.append(2) x.extend([3, 4]) x.insert(5, 6) x.sort()
  • Draw lists as folders § because they are mutable objects § can omit type to save space id 5 7 0 1 x id 8 ✗ 3 2 1 3 2 4 3 5 4 6 ✗ ✗ 6 3 4 7 1 2 3 3 4 5 6 8

Does not work for strings s = 'Hello World!' s[0] = 'J' ERROR s.append('?') ERROR See Python Standard Library for more methods

Lists vs. Objects With Attributes

List

  • Attributes are indexed § Example: a[2]

Point

  • Attributes are named § Example: p.x id 1 2 0 1 3 2 3 3 4 4 5 5 6 6 8 7 a id6 p id Point x 3. y 4. z 5. id

swap(x, 3, 4)

Lists and Functions: Swap

def swap(b, h, k): """Procedure swaps b[h] and b[k] in b Precondition: b is a mutable list, h " and k are valid positions in the list""” temp= b[h] b[h]= b[k] b[k]= temp Swaps b[h] and b[k], because parameter b contains name of list. 1 2 3 b h k temp swap:1 2 3 id7 3 4 id 3 4 0 1 1 2 5 3 9 4 3 5 2 6 0 7 x id ✗ ✗ 9 5 5 ✗ ✗ ✗

Slicing Lists Makes Copies

x = [5, 6, 5, Point(3,4,5)] y = x[1:3]

id 5 6 0 1 5 2 id 3 x id8 id 6 5 0 1 y id Point x 3. y 4. z 5. id

z = x[:]

id 5 6 0 1 5 2 id 3 z id

Lists and Strings: They go together like…

…a horse and carriage? Bread and butter? text = 'Rama lama lama\nke ding a de ding a dong' words = text.split() lines = text.split('\n') sep = '-' print sep.join(words) s = (sep.join(lines[0].split()) + ' ' + sep.join(lines[1].split())) text.split(sep): return a list of the words in text (separated by sep, or whitespace by default) sep.join(words): concatenate the items in the list of strings words, separated by sep. [‘Rama’, ‘lama’, ‘lama’, ‘ke’, …] returns a list of two strings ‘Rama-lama-lama-ke…’ ‘Rama-lama-lama ke-ding-a-de-ding-a-dong’ docsity.com

Foreshadowing: Iteration

  • To process a list, you often want to do the same thing to

each item in the list. Two ways to do this:

§ The map function: map(⟨ function ⟩, ⟨ list ⟩) § The for statement: for ⟨ variable ⟩ in ⟨ list ⟩: ⟨ statements ⟩ Call the function once for each item in the list, with the list item as the argument, and put the return values into a list. Execute the statements once for each item in the list, with the value of the variable set to the list item.