Lecture 6: October 15, 2019

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


  1. Greetings and felicitations!
  2. In more detail: how Python does function calls [quad.py]
    1. Caller suspends execution at point of call, remembers where it left off
    2. Formal parameters assigned values from actual parameters
    3. Execute function body
    4. Return control to where caller left off
  3. Refactoring code
    1. Compute the perimeter of a triangle [peri0.py]
    2. Collapse similar statements: make the distance between 2 points a function [peri1.py]
    3. Collapse similar statements: make the prompts a function [peri2.py]
    4. Refactor for clarity only: make the perimeter computation a function [peri3.py]
    5. Add error checking: “peri0.py” done right [peri-c.py]
  4. Add error checking: “quad.py” done right [quad-c.py]
  5. Sequences
    1. Sequences are a series of values in a particular order
    2. In Python predominantly strings and lists but also sets and tuples
  6. Strings
    1. Sequence of characters (characters are strings of length 1)
    2. Strings are immutable; really important for functions
  7. 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
  8. Indexing, var[position]
    1. Count from 0 to len(var)−1
    2. Position can be a negative number to count from right
  9. Assignment with indexing doesn’t work as strings immutable
    x = ’hEllo’; x[1] = ’e’ produces an error
  10. 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
  11. Looping over strings: for i in str
  12. Example program [strstuff.py]

UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: [email protected]
MISSING TITLE
Version of October 15, 2019 at 6:40PM

You can also obtain a PDF version of this.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh