To Lecture Notes

IT 211 -- Jan 16, 2019

Review Exercises

  1. What is a method? What are other names for a Python method?
    Ans: It is a block of Ruby code that is called by name, either as a standalone method (see next exercise) or from an object. Methods are also called procedures, functions, and subroutines.
  2. What is a standalone method?
    Ans: A method that is not called from object, such as round, min, len. An example of an instance method, or object method is upper. This method is called from an object like this: s.upper( ). It returns the string s converted to upper case.
  3. Write Python code that calls the str method. Ans:
    n = 37
    x = 3.5465
    s = "elephant"
    print(str(n) + " " + str(x) + " " + s)
    
  4. Construct the variable trace and predict the output for the Trace2 Example.
    Ans: The answer is at the bottom of the example script.
  5. Who was Edgar Dijkstra?
    Ans: He was a Dutch computer scientist that write the paper Goto Considered Harmful, out of which concept of structured programming. Structured programming means that all programs are written with three constructions: (1) sequence, (2) repetition (while loops), and (3) decision (if statements).
  6. Translate this pseudocode for computing the distance from the city center to an arbitrary location:
    Input number of blocks east or west from city center (x)
    Input number of blocks north or south from city center (y)
    Compute distance "as the crow flies" using Pythagorean Theorem.
    Print the distance.
    
    # Ans: Python Script:
    x = int(input("Input number of blocks east or west from city center: "))
    x = int(input("Input number of blocks east or west from city center: "))
    d = (x ** 2 + y ** 2) ** 0.5
    print("Distance as the crow flies: ", round(d, 2))
    
  7. Translate this pseudocode for counting to 20 in Python.
    Initialize counter to 1.
    while counter is less than or equal to 20
      Print value of counter.
      Add one to counter.
    end
    
    # Ans: Python script:
    counter = 1
    while counter <= 20:
       print(counter, end=" ")
       counter = counter + 1
    

Random Numbers