Outline for October 20, 2025

Reading: §4.5, 7, 9
Assignments: Homework 2, due October 29, 2025

  1. Greetings and Felicitations!
    1. Thursday’s office hours moved to 12:10pm–1:00pm (they are in person)

  2. Printing [printing.py]

  3. Recursion
    1. Tower of Hanoi [hanoi.py]

  4. Using random numbers
    1. import random
    2. Problem: compute π by tossing darts at a unit square
    3. First build routine to simulate dart toss at unit square [mc-1.py]
    4. Then build routine to see if co-ordinates are in unit circle [mc-2.py]
    5. Then build routine to read in number of tosses [mc-3.py]
    6. Put it all together [mc-4.py]
    7. Graphics! [mc-5.py]

  5. Files
    1. What is a file?
    2. What can you do with it? (For example, read, write, append)
    3. Types of files (text, binary)

  6. File Input and Output for text files
    1. Opening and closing: open(filename, mode), close()
    2. Reading: readline(), readlines(), read(), read(n)
    3. Writing: write(str), writelines(list)

  7. Exception EOFError — input function encounters an end of file

  8. Examples
    1. Print out a named file [fileio1.py]
    2. Print out a named file and prepend line numbers [fileio2.py]
    3. Store the output in filename.lst [fileio3.py]

  9. Examples
    1. Put lines in a file in random order [randlines.py]
    2. Read in a list of words from a file, then search it as requested; similar to linear search program [search-1.py]
    3. Now see how many words you checked total [search-1c.py]

  10. Dictionary
    1. Collection of key-value pairs

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

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

  13. Example: memos
    1. Remember how slowly the recursive Fibonacci number program rfib.py ran? Here is a faster recursive version that uses memos [rfibmemo.py]

  14. Example: word frequency count
    1. Unsorted [wfc-1.py]
    2. Sorted alphabetically [wfc-2.py]
    3. Sorted alphabetically, but dictionary order (note key=str.lower() in sorted [wfc-3.py]
    4. Sorted by frequency (treat lambda x: x[1] as an idiom to reference the value of the dictionary entry, not the key—to go from highest to lowest, replace x[1] with -x[1]) [wfc-4.py]
    5. Sorted by frequency first, then alphabetically—note use of function alphafreq(x); you can use any function here, and the parameter is the item [wfc-5.py]

  15. Handling exceptions
    1. except [except0.py]
    2. except exceptcode [except1.py]
    3. else [except2.py]
    4. except exceptcode as msgvar [except3.py]
    5. finally [except4.py]
    6. Exceptions in a function: who handles them? [except5.py, except6.py]
    7. Using global variables as error flags [except7.py]
    8. raise exceptcode message [except8.py]

UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
MHI 289I, Programming in Health Informatics
Version of October 24, 2025 at 10:43PM

You can also obtain a PDF version of this.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh