Outline for February 1

Reading: text, §5.2–5.3
Due: Homework 2, due on February 4 at 11:55pm — note extension


  1. String methods: find characters and substrings (return position or cause exception) cstrfind.py]
    1. S.find(s) — Return the index of the first occurrence of s in S; −1 if s not in S
    2. S.index(s) — Return the index of the first occurrence of s in S; ValueError exception if s not in S
    3. S.rfind(s) — Return the index of the last occurrence of s in S; −1 if s not in S
    4. S.rindex(s) — Return the index of the last occurrence of s in S; ValueError exception if s not in S
  2. String methods: miscellaneous [strmisc.py]
    1. S.count(s) — Return the number of times s occurs in S
    2. S.startswith(s)True if S starts with s
    3. S.endswith(s)True if S ends with s
    4. S.replace(s,t) — Replace all occurrences of s with t in S
  3. Lists
    1. Sequence of values (ints, floats, strings, other lists, etc.)
    2. Denoted by square brackets [ ] with values separated by commas
    3. Lists are mutable
    4. How to create a list
  4. Program to print words in a line [lines.py]
  5. Program to compute some statistics [addup.py]
  6. What you can do with lists
    1. Check membership: in, not in
    2. +: concatenation
    3. *: repetition
    4. list[a:b]: slice list from a to b−1
    5. del list[i]: delete element list[i]; i can be a slice
  7. Objects, references, aliasing
    1. For strings, one copy: assume a = "banana"
      1. After b = a or b = a[:], then a is b is True
    2. For lists, multiple copies: assume A = [ 1, 2, 3 ]
      1. After B = A, then A is B is True
      2. After B = A[:], then A is B is False
  8. list(enumerate(L)) produces pairs (index, list element)
  9. Lists as parameters: can change list elements in function and they are changed in caller [args2.py]
    1. Add elements to, remove elements: L.append(x), L.extend(ls), L.insert(i, x), L.pop(), L.remove(x)
    2. Element ordering: L.reverse(), L.sort()
    3. Other: L.count(x), L.index(x)
  10. Tuples
    1. Used to group data
    2. Like lists, but immutable


UC Davis seal
Matt Bishop
Office: 2209 Watershed Science
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
You can also obtain a PDF version of this.
Version of February 1, 2019 at 7:43PM