Outline for April 15, 2009

Reading: §4.1–4.3 Guest Lecturer: Justin Cummins

  1. Sequences
    1. Accustomed to use in for loops (for i in range(5):)
    2. Sequences are a series of values in a particular order
    3. In Python predominantly strings and lists but also sets and tuples
  2. Strings
    1. Sequence of characters (characters are strings of length 1)
    2. Special characters: newline ‘\n’ and tab ‘\t’
    3. Strings are immutable (also ints, floats, long ints); really important for functions
  3. Lists
    1. Sequence of values (ints, floats, long ints, strings, other lists, etc.)
    2. Denoted by square brackets “[]” with values separated by commas
    3. Lists are mutable
  4. raw_input()
    1. Does not make assumptions of input type
    2. Input always returned as string
  5. Basic string operations
    1. +, concatenation for like types (strings, lists)
    2. *, repetition repeats given value
    3. len() returns length of sequence
  6. Indexing, var[position]
    1. Count from 0 to len(var)−1
    2. Position can be a negative number to count from right
    3. Position/index illustrated in str_array.py
  7. Assignment with indexing, only works for lists because they are mutable
  8. 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
  9. Type conversion
    1. str(val) attempts to convert val to a string
    2. list(sequence) attempts to convert sequence to a list
  10. Program to assign letter grade when given score from 0 to 5: score.py
  11. Program to square each value in a list: squarelist.py