To Lecture Notes

IT 211 -- Jan 14, 2019

Review Exercises

  1. Find the errors in these Python constants:
    -4,563,432,873,332    4.73-e39   "dog'     
    "This is line 1./n"  false   
    
    Ans: 4,563,432,873,332    no commas allowed, it's okay to be 
                                 larger than 2 billion;
         4-e39                should be 4e-39;
         "dog'                quotes must be balanced, should 
                                 be "dog" or 'dog';
         'This is line 1./n'  should be 'This is line 1.\n' for 
                                 the \n to acts as a new line character.
         false                should be False, Python is case sensitive.
    
  2. If value is defined by
    value = 3.14159265
    
    Write a statement to round off value to four digits after the decimal point.
    Ans: Use this statement to round to four digits after the decimal point and print the result:
    print(round(value, 4))
    
  3. What is the value of x after each of these expressions?  Check your answers with Interactive Python. Use this Python Operator precedence table to help you.
    1.   x = 4 + 2 // 5 + 3 * 5 ** 3  Ans: 379
    2.   x = "cat" * 3 + "mouse" + "dog" * 2
        Ans: catcatcatmousedogdog
    3.   x = (4 + 9) % 5  Ans: 3
  4. Construct the variable trace and predict the output. Check your answer by creating a Python script and running it. Recall that // means integer division; % means the remainder from integer division.
    a = 3
    b = 5
    a = 2 * a + b
    b = a + 3 * b
    a = a // 2
    b = b // 3
    print("sum = ", a + b)
    
    Ans:  
    Variable trace:   Output:
    
      a    b          sum =  13  
    ----+----
      3 |  5
     11 | 26
      5 |  8
    
  5. Translate this formula into a Python assignment statement:

      a - 2b / 7
    s = 
      c + (d - 5e)-x + 3y

    This exercise should help you complete Project 1. Hint: don't use unnecessary parentheses.
    Ans: s = (a - 2 * b / 7) / (c + (d - 5 * e) ** (-x + 3 * y))
    
  6. Einstein's theory of Relativity predicts that if the velocity of an object increases, the actual mass of the object increases as well according to this formula:
          m = m0 / √[1 - (v/C)2]
    m is the actual mass, m0 is the rest mass, v is the velocity, and C is the speed of light, equal to 2.998 × 108 meters per second. (This increase of mass with increasing velocity is because of Einstein's formula E = mc2. You might enjoy this related Far Side cartoon.)

    Write a Python program that computes the actual mass, with inputs rest mass and velocity.  Define the velocity of light C as a constant. (By convention, the name of a Python constant starts with an uppercase letter.) A consequence of this formula is that no object can travel faster than the speed of light. Validate the input velocity to insure that it is less than the speed of light. Validate the rest mass m0 to insure that it is nonnegative. Here is some pseudocode:
    Define velocity of light as C = 2.998e8
    Input rest mass
    If rest mass < 0
        Print error message
        Exit script
    End if
    
    Input velocity
    If velocity >= C
        print error message
        exit script
    End if
    
    Compute actual velocity from rest mass
        and velocity using Einstein's formula.
    Print actual velocity.
    
    Ans:
    import sys
    C = 2.998e8
    
    # Input and validate rest mass.
    m0 = float(input("Enter rest mass" "))
    if m0 < 0:
        print("Rest mass must be nonnegative: ")
        sys.exit( )
    
    # Input and validate velocity.
    if v >= C:
        print("Velocity must be < C: ")
        sys.exit( )
    
    # Compute and print actual mass.
    m = m0 / (1 - (v / C) ** 2) ** 0.5
    print("Actual mass:", m)
    
    # You can also write the formula for actual mass like this:
    import math
    m = m0 / math.sqrt(1 - (v / C) ** 2)
    

Builtin Methods

Control Structures

Pseudocode

Project 2