To Lecture Notes

IT 212 -- Sep 21, 2020

Review Exercises

  1. Zoom Poll 3 Review Exercises. Correct answers are marked with *:
    1. What is the output?
      print(chr(0b00100011))
      
      *a. #        b. $        c. %         d. *
    2. In this method definition
      def f(n):
          return n * n
      
      the identifier n is a(n) ________ .
      a. argument                 b. local variable
      c. instance variable     *d. parameter
    3. What kind of method is open as invoked in this statement:
      fin = open("input.txt", "r")
      
      a. instance method     *b. standalone method
    4. What kind of method is close as invoked in this statement:
      fin.close( )
      
      *a. instance method     b. standalone method
    5. What is the Python name for a source code file?
      a. directory    b. input file    c. manifest    *d. module
  2. Rewrite the script from last week to put the definition of get_greeting in one module, and the line that invokes it in a different module:
    def get_greeting(the_name):
        return "Hello, " + the_name + ", how are you?"
        
    print(get_greeting("Larry"))
    
    This is a traditional test of the get_greeting method.
    Ans: here are the greeting and test1 modules:
    # ----- Source code file: greeting.py 
    def get_greeting(the_name):
        return "Hello, " + the_name + ", how are you?"
    
    # ----- Source code file: test1.py 
    from greeting import get_greeting
    print(get_greeting('Larry'))
    
  3. What is the difference between
    # Method 1
    from modulename import methodname
    
    and
    # Method 2
    import modulename
    
    Ans: with Method 1, the method does not need to be fully qualified. with Method 2, the method must be fully qualified by prefixing it with the module name. With Method 2, the test2.py module looks like this:
    import greeting
    print(greeting.get_greeting('Larry'))
    

Unit Tests

Reading from Files

Refactoring

Practice Problems

Project 2