To Lecture Notes

IT 212 -- Oct 7, 2020

Review Exercises

  1. Answer these three Zoom Poll questions. The correct answer is marked with *:
    1. What are the meanings of these Python operators?
      =  ==
        a. numeric comparison; string comparison
        b. bitwise comparison; assignment
        c. object comparison; numeric comparison
      *d. assignment; comparison
    2. What are the meanings of these Python operators?
      and  &
        a. logical or; bitwise exclusive or
      *b. logical and; bitwise and
        c. logical bitwise or; concatenationbitwise
        d. and; concatenation
    3. What are the meanings of this Python operator?
      *
        a. multiplication and exponentiation
      *b. multiplication and repetition
        c. multiplication only
        d. exponentiation only
  2. How many stars do these for loops print?
    # a. Sequential for loops.
    for i in range(0, 3):
        print('*')
    for i in range(0, 2):
        print('*')
    # Ans: Loops are sequential, so add: 
    #      3 + 2 = 5.
    
    # b. Nested for loops.
    for i in range(0, 3):
        for j in range(0, 2):
            print('*')
    # Ans: Loops are nested so multiply: 
    #      3 * 2 = 6
    
    # c. Watch out for the indentation.        
    for i in range(0, 9):
        print('*')
        print('*')
    print('*')
    # Ans: 9 * 2 + 1 = 19
    
  3. Answer these five Zoom Poll questions about for loops.
    1. for i in range(0, 200):
          print("*")
      for j in range(0, 300):
          print("*")
      for k in range(0, 500):
          print("*")
      
           a. 200    b. 300    c. 500    *d. 1,000
    2. for i in range(0, 200):
          for j in range(0, 300):
              for k in range(0, 500):
                  print("*")
      
           a. 500   b. 1,000   c. 300,000   *d. 60,000,000
    3. for i in range(0, 200):
          for j in range(0, 300):
              print("*")
      for i in range(0, 500):
          print("*")
          print("*")
      print("*")
      
           a. 1,000    b. 1,003    *c. 61,001    d. 30,000,000
    4. The class A has both __str__ and __repr__ dunder methods defined. For the statements
      a = A( )
      print(a)
      
      which dunder method is used to represent a as a string?
      *a. __str__       b. __repr__
    5. The class A has both __str__ and __repr__ dunder methods defined. For the statements
      a = A( )
      print([a])
      
      which dunder method is used to represent a as a string within the list?
      a. __str__       *b. __repr__

BankAccount Class

  1. Here are the instance members obtained for the BankAccount class by the breakout groups in class yesterday (Oct 5):
         Instance Variables: acct_num  acct_type  name  email  balance
         Instance Methods: __init__  deposit  withdraw  __str__
  2. UML Diagram:
    +-----------------------------------------------------+
    |                     BankAccount                     |
    +-----------------------------------------------------+
    | + acct_number : int                                 |
    | + acct_type : str                                   |
    | + name : str                                        |
    | + email : str                                       |
    | + balance : float                                   |
    +-----------------------------------------------------+
    | - __init__(self : BankAccount, acct_number : int,   |
    |         acct_type : str, name : str, email : str)   |
    | + deposit(self : BankAccount, amt : double) : bool  |
    | + withdraw(self : BankAccount, amt : double) : bool |
    | - __str__(self : BankAccount) : str                 |
    +-----------------------------------------------------+
    
  3. Source Code with Errors:
    BankAccount Class   Test1 Module
    Each source code module has about 10 errors. Fix the errors.

Practice Problem

  1. Using the Person class (see the Person Example), create and print list of Person objects from the data in the input file persons.txt. Once you get your script (named test3.py) working, try it on this larger input file: kids.txt. This second file uses space delimiters, not comma delimiters.
    Ans:
    # Module: person.py
    class Person:
    
        def __init__(self, the_name, the_gender, the_age):
            self.name = the_name
            self.gender = the_gender
            self.age = the_age
    
        def have_birthday(self):
            self.age += 1
    
        def __str__(self):
            return f"{self.name};{self.gender};{self.age}"
    
        def __repr__(self):
            return str(self)
    
    #----------------------------------------------------
    # Module: test3.py
    from person import Person
    
    # Initialize list of Person objects
    persons = [ ]
    
    fin = open("persons.txt", "r")
    print(fin)
    
    # Throw away first line
    fin.readline( )
    
    for line in fin:
        fields = line.split(",")
        # strip eliminates leading or trailing white
        # space
        name = fields[0].strip( )
        gender = fields[1].strip( )
        age = int(fields[2].strip( ))
        p = Person(name, gender, age)
        persons.append(p)
    
    fin.close( )
    print(persons)
    #------------------------------------------------------
    
    To read the kids.txt file, replace the line
    fin = open("persons.txt", "r")
    
    by
    fin = open("persons.txt", "r")
    
    Also replace the line
    fields = line.split(",")
    
    by
    fields = line.split(" ")
    

Sorting