To Lecture Notes

IT 211 -- Jan 28, 2019

Review Exercises

  1. What are the differences among these Python scripts?
    # Script a 
    x = 1 
    y = 2
    while x < 30 and y < 30:
        x = 2 * x + 3 * y    
        y = 3 * x + 2 * y
        print(f"{x} {y}") 
    
    # Script b 
    x = 1 
    y = 2
    while x < 30 and y < 30:
        print(f"{x} {y}")
        x = 2 * x + 3 * y    
        y = 3 * x + 2 * y 
        
    # Script c
    x = 1 
    y = 2
    while x < 30 and y < 30:
        x = 2 * x + 3 * y
        y = 3 * x + 2 * y 
    print(f"{x} {y}")
    
    # Script d
    x = 1 
    y = 2
    print(f"{x} {y}")
    while x < 30 and y < 30:
        x = 2 * x + 3 * y
        y = 3 * x + 2 * y
    
    # Ans:  Variable trace:
      x    y
    ----+-----
      1    2
      8   28
    100  356
    
    # The output for Script a is
    8 28
    100 356 
    The print statement is at the end of the loop in Script a.
    the values 1 2 are not printed.
    
    # The output for Script a is:
    1 2
    8 28
    Ans: the print statement is at the beginning of the loop.
    so the values 1 2 are printed but the final values are not printed.
    
    # The output for Script c is:
    100 356
    The print statement is after the end of the loop, so
    the final values are printed.
    
    # The output of Script d is:
    1 2
    The print statement is before the beginning of the loop.
    Only the initial values of 1 2 are printed. The computations 
    in the loop here are wasted because nothing from it is printed.
    

Comparison and Logical Operators

File Input and Output

Sequential Processing