Outline for November 5, 2020
Reading
: §4, 9
Due
: Homework 3, due November 13, 2020
Refactoring code (
continued
)
Collapse similar statements: make the distance between 2 points a function [
peri1.py
]
Collapse similar statements: make the prompts a function [
peri2.py
]
Refactor for clarity only: make the perimeter computation a function [
peri3.py
]
Add error checking: “peri0.py” done right [
peri-c.py
]
Dictionary
Collection of key-value pairs
Creating dictionaries
Using
d = {}
Using
d = dict()
Methods for dictionaries
k in D
: True if dictionary
D
has key
k
; else False
D.keys()
: list of keys in
D
D.values()
: list of values in
D
D.items()
: list of tuples (key, value) in
D
D.get(k, d)
: if key
k
in
D
, return associated value; else return
d
del D[k]
: delete tuple with key
k
from
D
D.clear()
: delete all entries in
D
Sorting the dictionary
sorted
sorts based on keys
Example: word frequency count
Unsorted [
wfc-1.py
]
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.