Professional Documents
Culture Documents
Doc1 2
Doc1 2
x = “Hi there”
x = 10 while x > 0:
print x
x=x-3
4 lines
a) for i in range(10):
print “hi”
b) for i = 1 to 10: print “hi”
print i + j
9. How many lines does the following program print?
print “hi”
15
10. Which of the following while loops would continue to loop as long as num is in the range 3 to 12,
exclusive?
a) while num > 12 and num < 3: # do something with num
b) while num < 12 or num > 3: # do something with num
sum = 0
for i in range(3):
12. Which of the following for loops would print the following numbers?
3
5
7
9
a) for i in range(3, 10, 2):
print i
b) for i in range(3, 9, 2):
print i
c) for i in range(9):
print i
d) for i in range(3,9):
13. Which of the following for loops would print the following numbers?
012345
for i in range(6): if i == 3:
continue print i
15. Which of the following Python programs creates a list with the numbers 1 through 5? a) my_list =
(1, 2, 3, 4, 5)
b) my_list = [1, 2, 3, 4, 5]
c) my_list = 1, 2, 3, 4, 5
d) my_list = “1, 2, 3, 4, 5”
18. Which of the following lines of code will cause an error? Use the following definition of ages:
ages = (12, 5, 8)
a) ages = ages + (1, 3, 5)
b) print ages[2]
c) ages = ages[2:] d) ages[0] = 3
20. What is the value of num after this code runs?