Lecture 8: October 24, 2019

Reading: §8
Due: Homework 2, due on October 24 at 11:59pm


  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. Lists and strings [datecvt.py]
  3. Program to print words in a line [lines.py]
  4. 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
  5. Searching a list
    1. Example use: linear search [linsearch.py]
  6. 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
  7. 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)
  8. More on parameters: named arguments and variable number of arguments [args3.py]
  9. Tuples
    1. Used to group data
    2. Like lists, but immutable
  10. isinstance(obj,type) function
    1. type is bool, float, int, list, str, tuple

UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: [email protected]
MHI 289I, Programming in Health Informatics
Version of October 23, 2019 at 9:52PM

You can also obtain a PDF version of this.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh