Outline for October 26, 2023

Reading: §7
Due: Homework 2, due October 26, 2023


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

  2. 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)

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

  4. 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]

  5. 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]

  6. Dictionary
    1. Collection of key-value pairs

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

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

  9. 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]

UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
ECS 235A, Computer and Information Security
Version of October 25, 2023 at 10:08PM

You can also obtain a PDF version of this.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh