To Lecture Notes

IT 211 -- May 13, 2019

Review Exercises

  1. What are the Python names for these datatypes: array, dictionary, floating point, integer, logical (True or False).
    Ans: list, dict, float, int, bool
  2. How are these symbols used in Python:
    +  *  /  \  =  !  #  .  ( )  [ ]  { }
    
    Ans: +: addition, concatenation, assignment operator (+=).
    *: multiplication, exponentiation, repetition, assignment operator (*=).
    /: floating point division, integer addition (//), assignment operators (/* and //*).
    \: line continuation.
    =: assignment operators (=), comparison operators (==, <=, >=, !=), assignment operators (+=, -=, *=, /=, //=, %=).
    !: not as in not equal (!=).
    #: source code comment.
    .: member selection operator, for example s.upper( ), p.have_birthday( ), p.age.
    ( ): grouping in formula to change order of operations, method definition, method invocation, tuple definition.
    [ ]: list definition, look up item in list by index (print(a[2])).
    { }: dictionary definition, look up item in list by key (print(d["C4544"]).
  3. Explain the difference between the __init__ method and a class constructor.
    Ans: The constructor is a standalone method generated by the Python interpreter. It always has the same name as the name of its class. A constructor creates a new object of its class, and then immediately calls the __init__ method of the class to set the values of the instance methods.
  4. For a class object p, the __str__ method should not be invoked like this
    print(p.__str__( ))
    
    because it is private. (Actually, there is nothing stopping you from accessing __str__ in this way, but it would be bad manners to do so because the __str__ method is private. The concept of private is on the honor system in Python.  Other languages like Ruby, C++, Java, and C# inforce privacy.) How should this method be invoked by p?
    a. p.str( )      b. str(p)      c. str(print(p))      d.   __str__(p)
    Ans: b
  5. In the Card Example, identify these items in the Card class and test1 test files:

         Class Name   Instance Variable   Instance Method   Public Member

         Private Member  Dunder Method   Constructor   Local Variable 

         Standalone  Method   Parameter   Argument

    Ans: Here are identified items  for each example class.
  6. Draw the variable trace table and predict the output for this script using card.py from the Card Example.
    from card import Card
    a = Card(13, "H")
    b = Card(11, "C")
    print(b, "b", a, "a", end=" * ")
    print(a.color( ), b.color( ))
    b.rank = 3
    print(a.rank)
    b.suit = "S"
    print(b.color( ) + str(b))
    
    Ans:
    Card object a:  Card object b:
     rank   suit    rank   suit
    ------+------++------+-------
      13     "H"     11     "C"
       3     "S"
    
    Output:
    JCbKHc * red black
    3
    black3S  
    

Review for Final