To Lecture Notes

IT 211 -- Mar 6, 2019

Review Exercises

  1. Of the aggregate datatypes list, tuple, dict, and a user defined class, which are mutable and which are immutable? Ans:

    Datatype Mutability
    list Mutable
    tuple Immutable
    dict Mutable
    class Mutable

  2. What is the advantage of a dictionary instead of a list or tuple to represent an object?
    Ans: With a dictionary, one can look up information by key, which is usually easier to read than looking up by index.
  3. What is the advantage of using a class over a dictionary to represent an object?
    Ans: With a class, the lookup is even easier to understand than it is for dictionaries. For a class: p.age, for a dictionary: p["age"].
  4. What is the difference between a standalone method and an instance method?
    Ans: a standalone method is not called from an object, for example,
    print(n)
    
    An instance method is called from an object, for example,
    # The instance variable upper is called from the object s.
    u = s.upper( )
    
  5. Write scripts to do the following with this cinderella.txt file:
    1. Convert entire file to upper case.
    2. Count the number of words in the file.
  6. What is a constructor?
    Ans: A constructor is a method that creates an object, for example:
    n = int( )
    s = str( )
    p = Person("Alice", "F", 11)
    
  7. What is a dunder method?
    Ans: A method with a double underscore prefix and double underscore suffix, for example:
    __init__  __str__
    
  8. Look at the example class Pet files pet.py and test1.py, identify these items in each of the preceding classes and test files:

         Class Name   Instance Variable   Instance Method   Public Member

         Private Member  Dunder Method   Constructor   Local Variable 

         Standalone  Method   Parameter   Argument

    We worked on this Exercise in class today.  Try to identify these items yourself before looking at the answers.

Turtle Graphics