Outline for October 14, 2024

Reading: text, §6.1–6.8
Due: Homework 1, due October 14, 2024


  1. Sequences
    1. Sequences are a series of values in a particular order
    2. In Python predominantly strings and lists but also sets and tuples

  2. Strings
    1. Sequence of characters (characters are strings of length 1)
    2. Strings are immutable; really important for functions

  3. Basic string operations
    1. +, concatenation for strings
    2. *, repetition repeats given value
    3. len() returns length of sequence
    4. s in str returns True if s is a substring of str, False otherwise

  4. Indexing, var[position]
    1. Count from 0 to len(var)−1
    2. Position can be a negative number to count from right

  5. Assignment with indexing doesn’t work as strings immutable x = "hEllo"; x[1] = "e" produces an error

  6. Slicing, var[start:end]
    1. Value at index end not included in slice
    2. If omitted, starting value defaults to 0 and ending value defaults to last index + 1
    3. Can use negative index

  7. Looping over strings: for i in str

  8. Example program [strstuff.py]

  9. 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

  10. 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

  11. Example of sets [sets.py]

UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: [email protected]
ECS 235A, Computer and Information Security
Version of October 12, 2024 at 9:22PM

You can also obtain a PDF version of this.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh