next up previous
Next: About this document ...

  1. Give the computed output:

           print 9/2
           str = "word"
           print str[0:2]
           print str[-1]
    ************************** OUTPUT ******************************
    >>> print 9/2
    4
    >>> str = "word"
    >>> print str[0:2]
    wo
    >>> print str[-1]
    d
    ****************************************************************
    

  2. Give the output of the following program:
           L = [1,2,3]
           M = ['cat', L, 'dog']
           L[2] = 'canary'
           print M
           L = [1,2,3]
           M = ['cat', L[:], 'dog']
           L[2] = 'canary'
           print M
    ************************** OUTPUT ******************************
    ['cat', [1, 2, 'canary'], 'dog']
    ['cat', [1, 2, 3], 'dog']
    ****************************************************************
    

  3. The raw_input command accepts a prompt and then reads a string from the keyboard. Complete the python program below so that it uses a loop to print the word that is entered backwards:

           word = raw_input("Type a word:  ")
           print word
    ************************ One Solution **************************
          1 if __name__ == "__main__":
          2   word = raw_input("Type a word:  ")
          3   length = len(word)
          4   for ch in range(0, length):
          5     print word[-1]
          6     t = len(word)-1
          7     word = word[0:t]
    ****************************************************************
    

  4. The program below lists the arguments passed in from the command line. Rewrite this program so that it accepts two arguments from the command line, converts them to integers and prints their sum.

    import sys
    
    if __name__ == "__main__":
        for count in range(0, len(sys.argv)):
           print sys.argv[count]
    ************************ One Solution **************************
          1 import sys
          2
          3 if __name__ == "__main__":
          4   if len(sys.argv) != 3:
          5     print "usage: ", sys.argv[0], " <arg1> <arg2>"
          6     sys.exit()
          7   arg1 = int(sys.argv[1])
          8   arg2 = int(sys.argv[2])
          9   print "The sum of %d + %d = %d" % (arg1, arg2, arg1+arg2)
    ****************************************************************
    

  5. (1 point extra credit) What will the following GUI look like:
    from Tkinter import *
    win = Frame()
    Button(win, text = 'Button3').pack(side=BOTTOM)
    Button(win, text = 'Button2').pack(side=BOTTOM)
    Button(win, text = 'Button1').pack(side=BOTTOM)
    win.pack()
    win.mainloop()
    





Brian Malloy 2006-05-25