To Lecture Notes

IT 212 -- Oct 26, 2020

Review Exercises

  1. Define the input string
    s = 'cmkpeiqwpcaoeuvnmlpopl'
    
    and the regular expression
    r = r'[aeiou]+'
    
    After importing the Python module re, what is the output of each of these statements:
    1. print(re.search(r, s))
      Ans: <re.Match object; span=(4, 6), match='ei'>
    2. print(re.findall(r, s))
      Ans: ['ei', 'aoeu', 'o']
    3. print(re.split(r, s))
      Ans: ['cmkp', 'qwpc', 'vnmlp', 'pl']
    4. print(re.sub(r, "*", s))
      Ans: cmkp*qwpc*vnmlp*pl
  2. What is the difference between the tuple (3, 5, 7) and the list [3, 5, 7]?
    Ans: The main difference is that tuples are immutable, which means that the can't be changed.
  3. Show that the tuple (3, 5, 7) is immutable.
    Ans: Try these statements:
    t = (3, 5, 7)
    # Accessing tuple elements with t[1] is okay.
    print(t, t[1])
    # Changing tuple elements is not okay,  the following
    # statement will produce this error:
    # "TypeError: 'tuple' object does not support item assignment"
    t[1] = 9
    

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

Lab 3