Week 16 agenda -- last class and quiz

11 Dec 2011

We'll have one last quiz, which will cover all the major Python concepts we've talked about:

  • variables and types
  • for loops
  • lists
  • conditionals
  • working with files

You'll have to have all homework in by Thursday night.

Quiz examples

  1. Recall that sys.argv is a list containing all the words you type on the command line. What does the following program do?

    import sys
    num = 1
    for x in sys.argv:
        print(num, x)
        num = num + 1
  2. Fill in the definition of the following function so that it prints even if the input is an even number, and odd if the input is an odd number. The % operator can be used to compute the remainder when dividing one number by another -- for example, 11 % 3 gives 2, because 11 divided by 3 has remainder 2.

    def print_parity(x):
  3. Explain in words (you don't have to write any code) how to write a program that reads a file, and then overwrites the file with the same contents except all in uppercase.

  4. What is the value of x at the end of the following program?

    a = 1
    b = 2
    if a > b:
        x = a
        x = b
  5. Write a function that takes 2 numbers as input and returns the largest of the 2 numbers.

  6. Fill in the following function so that it takes as list of numbers as input, and returns a new list consisting of the original numbers that were positive.

    def positives(numbers):
        output = []
        for n in numbers:
           if ...:
        return ...
  7. Define a function called triangle that prints a triangle of height n when given input n. For example, triangle(4) should print something like: