Comp 150 Exam 1 Study Guide

Exam 1 is Wednesday, Feb 20, 2019

Sections of Python: 

There will be a couple simple python functions to write on the exam. You will also be asked to describe the OUTPUT of some simple python functions. 

answers will be coming soon.

You will receive this Python summary on the exam.

1. What is printed by the following?

print('{} * {} = {}'.format(x,y,x*y))
print('+'*x + "hello" + '+'*y)

2. What is the value of the number s after execution of the following python code:
while n<7:
   s = s + n*n
   n = n+2
3. What is the value of L after execution of the following python code?  Note L is a list.
n = 0
L = []
while n < 5:
    L = L + [2*n]     # same as L.append(2*n)
    n = n + 1

4. What are the values of x and y at the end of the following loop:

while y < 20:
     y = y+x
     x = x+1

Hint: make a table of x,y values as of the start of the loop. Initially (x,y) = (1,0); after one time through (x,y) = (2,1)

5. What is printed by the following?
for i in range(1,5):

6. What is printed by the following? There is one pair of numbers per line.

for i in range(1,5):
    for j in range(0,i):

7. What is string t after the following? Note that the last line adds each letter of s in turn to the end of t:
t = ""
s = "foobar"
for i in range(len(s)):
    t = t + s[i]

8. Write a python function to multiply all the numbers in a list L:
def mulnums(L):
mulnums([2,3,7]) should return 42.
9. Suppose we define

    def filter(L):                  # L is a list of numbers
       res = [ ]                    # an empty list
       for x in L:
          if x % 2 == 1: res = res + [x]         # the condition here is true if x is odd
       return res                                # res = res+[x] is the same as res.append(x)

What is returned by filter([1, 2, 4, 5, 7, 12, 16, 17])?

10. Below is a while loop. Convert it to a for loop, with an appropriate range().

while i < 6:
    sum += i
    i += 1