Outline for February 7, 2018

Reading: §8


  1. 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
  2. Program to print words in a line [lines.py]
  3. 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
  4. 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
  5. enumerate(L) produces pairs (index, list element)
  6. 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)
  7. Tuples
    1. Used to group data
    2. Like lists, but immutable
  8. Recursion
    1. n factorial [nfact.py]
    2. Fibonacci numbers [rfib.py]
    3. Sum of digits [sumdigits.py]

Matt Bishop
Department of Computer Science
University of California at Davis
Davis, CA 95616-8562 USA
Last modified: Version of February 6, 2018 at 8:10PM
Winter Quarter 2018
You can get a PDF version of this