To Lecture Notes

IT 212 -- Oct 21, 2020

Review Exercises

  1. What is wrong with this __lt__ method?
    def __lt__(self, other):
        if self.name < other.name:
            return "True"
        else:
            return "False"
    
    Ans: The __lt__ method must return a bool value, not an str value. Both "True" and "False" both convert to True when converted to bool values. (Only the empty string "" converts to False.) Do this instead:
    def __lt__(self, other):
        if self.name < other.name:
            return True
        else:
            return False
    
    Furthermore, we can replace the if statement with a simpler return statement:
    def __lt__(self, other):
        return self.name < other.name:
    
  2. A dictionary d is defined as d = dict( ), which is the same as d = { }. Write a statement that adds the value 12 to d using the key "age". Ans:
    d["age"] = 12
    
  3. How do you check if "address" is being used as a key in the dictionary d?
    Ans: run the statements
    print("address" in d, "age" in d)
    # Output:
    False True
    
    This is because the key "address" is not contained in the dictionary d but the key "age" is contained in d.
  4. Add missing punctuation to these nested dictionaries to make syntactically correct Python code:
    persons = \
       { 1001 : { name : Alice,
                  address : { street : 123 Main Street
                              state : IL
                              zipcode : 60019 }
                  phone : { areacode : 123,
                            number : 333-4544 }
               }
         1002 : { name : Bob,
                  address : { street : 678 Walnut Street
                              state : MI
                              zipcode : 48123 }
                  phone : { areacode : 456,
                            number : 777-9753 }
                }
         }
    print(persons)
    
    # Corrected version:
    persons = \
    { 1001 : { "name" : "Alice", 
               "address" : { "street" : "123 Main Street", 
                             "state" : "IL", 
                             "zipcode" : 60019 }, 
               "phone" : { "areacode" : 123, 
               "number" : "333-4544" } 
             },
      1002 : { "name" : "Bob",
               "address" : { "street" : "678 Walnut Street", 
                             "state" : "MI", 
                             "zipcode" : 48123 }, 
               "phone" : { "areacode" : 456, 
               "number" : "777-9753" } 
             } 
    }
    
  5. After correcting the punctuation in the previous exercise, write statements that
    1. prints the person with key 1001.
      Ans: print(persons[1001])
    2. prints the address of the person with key 1001.
      Ans: print(persons[1001]['address'])
    3. prints the zipcode of the person with key 1001.
      Ans: print(persons[1001]['address']['zipcode'])
  6. Look at the Pet dictionary example (PetsDict Example).
  7. What do each of these regular expression tokens mean?
    \d  \w  \A  \Z  {3}  ?
    *  +  |  [1-9]  [a-z]
    
    Ans:
    \d - any digit 0 to 9
    \w - word: any digit or letter
    \A - beginning of string
    \Z - end of string
    {3} - repeat three times, e.g., \d{3}
    ? - repeat 0 or 1 time (optional)
    * - repeat 0 or more times
    + - repeat 1 or more times
    | - or
    [1-9] - digits 1 to 9
  8. For each regular expression, which of the strings have at least one match? There may be more than one correct answer for each question.
    1. \ABS*T+D\Z
      (a) ABSSSTDZ     *(b) BTTTTTTD      *(c) BSTD       (d) BTSSSD
    2. AG+C+T (ACGT are the letters of the DNA alphabet). There is no \A and \Z so we are looking to match a substring.
      (a) ACGT            *(b) DUAGGCTU      (c) SRAGTTTP     * (d) WAGCTUUU
    3. \A([1-9]|11|12)(:[0-5]\d){2}\Z (legal time)
      (a) 7:85:13      *(b) 12:31:04      (c) 12:31:4       *(d) 4:12:09
    4. \A[_a-z][_a-z0-9]*\Z  (legal Python local variable names)
      *(a) _n234          *(b) num_customers   (c) numCustomers   (d) 8abc_xyz
  9. Write a Python script that checks the results of Exercise 3. Ans:
    import re
    
    # Exercise 3a
    regex = r"\ABS*T+D\Z"
    inputs = ["ABSSSTDZ", "BTTTTTTD", "BSTD", "BTSSSD"]
    for string in inputs:
        print(string, re.search(regex, string))
    print( )
    
    # Exercise 3b
    regex = r"AG+C+T"
    inputs = ["ACGT", "DUAGGCTU", "SRAGTTTP", "WAGCTUUU"]
    for string in inputs:
        print(string, re.search(regex, string))
    print( )
    
    # Exercise 3c
    regex = r"\A([1-9]|11|12)(:[0-5]\d){2}\Z"
    inputs = ["7:85:13", "12:31:04", "12:31:4", "4:12:09"]
    for string in inputs:
        print(string, re.search(regex, string))
    print( )
    # Exercise 3d
    regex = r"\A[_a-z][_a-z0-9]*\Z"
    inputs = ["n234", "num_customers", "numCustomers", "8abc_xyz"]
    for string in inputs:
        print(string, re.search(regex, string))
    

The re Module

Reading from the Web

  1. Look at the this webpage:
         http://facweb.cdm.depaul.edu/sjost/it212.greeting.htm
    Write Python script to read the contents of this webpage and print it to the console.
    Ans: Script for Item 1
  2. Write a script to read the contents of this page and load the contents into the dictionary d.
         http://facweb.cdm.depaul.edu/sjost/it212/rates.txt
    Ans: Script for Item 2.
  3. Write a script to read the contents of this JSON page and load the contents into a dictionary using JSON serialization.
         http://facweb.cdm.depaul.edu/sjost/it212/rates.json.txt
    Ans: Script for Item 3

Structured Query Language and SQLite

SQL via Python