Outline for November 5, 2020

Reading: §4, 9
Due: Homework 3, due November 13, 2020

  1. Refactoring code (continued)
    1. Collapse similar statements: make the distance between 2 points a function [peri1.py]
    2. Collapse similar statements: make the prompts a function [peri2.py]
    3. Refactor for clarity only: make the perimeter computation a function [peri3.py]
    4. Add error checking: “peri0.py” done right [peri-c.py]

  2. Dictionary
    1. Collection of key-value pairs

  3. Creating dictionaries
    1. Using d = {}
    2. Using d = dict()

  4. Methods for dictionaries
    1. k in D: True if dictionary D has key k; else False
    2. D.keys(): list of keys in D
    3. D.values(): list of values in D
    4. D.items(): list of tuples (key, value) in D
    5. D.get(k, d): if key k in D, return associated value; else return d
    6. del D[k]: delete tuple with key k from D
    7. D.clear(): delete all entries in D

  5. Sorting the dictionary
    1. sorted sorts based on keys

  6. Example: word frequency count
    1. Unsorted [wfc-1.py]

UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
MHI 289I, Programming for Health Informatics
Version of November 5, 2020 at 11:59PM

You can also obtain a PDF version of this.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh