Suggested Homework #5 (for Quiz #5 on 02/12/08)

Problem 1: Suppose that the following was entered into the Python shell. For each entry, the interpreter responds with either: (a) the value of the object computed, (b) nothing, if it is an assignment (name binding), or the value computed is None , or, (c) an error message due to an exception. Fill in the blanks ("___") with how the Python interpreter would respond if each of the below was entered in the sequence shown. Note that you could just enter this sequence into the interpreter and see what happens, but this is a luxury you will not have during the quiz.
>>> 2 + 4.2 
    ________
    
>>> "this " + "is"*4 + "easy"
    ________
    
>>> (1,2.2,"a") + (3,4.5,"b")
    ________
    
>>> z = ("a",3) * 5
    ________

>>> q = z[3][0] * z[2][1]
    ________

>>> y = [1,2,"3",[4,6]] + ["a","b","c"]
    ________

>>> y[2:6]
    ________

>>> y[3:]
    ________

>>> y[3][:]
    ________

>>> i,p = 1,1
    while (i<5):
       i,p = i+1,p*i
    
    p
    ________
    
>>> r = range(5,15)
    r
    ________

>>> for i in r:
      p += i

    p
    ________

>>> def f(x):
      if x: 
	return x+a
    ________
    
>>> f(0)
    ________

>>> f(10)
    ________

>>> f("")
    ________

>>> f("a")
    ________

>>> p = q
    ________

>>> f(0)
    ________

>>> f(10)
    ________

>>> f("")
    ________

>>> f("a")
    ________
Problem 2: Call-by-value. Repeat the above for the next few entries (again entered in sequence).

>>> def foo1(x,y):
	x = x + y
    
>>> def foo2(x,y):
	x[len(x):] = y

>>> p = [1,2,3,4]

>>> foo1(p,"abc")
    
    p
    ________

>>> foo2(p,"abc")

    p
    ________
    
>>> q = ["a","b","c"]

>>> foo1(q,[1,2,3])

    q
    ________
    
>>> foo2(q,[1,2,3])

>>> q
    
    ________