Learning Python
Final Exam
Student name:
1) Python was created by (circle one):
2) To traverse a directory tree in Python, you would be
most likely to use:
3) Python is useful for:
4) In the space below, write a Python program that prints the string “Spam” 10 times on the standard output stream.
5) Now, write the same program as in question 4 again,
but print the string “Spam X”, where the “X” is replaced with the current value
of a loop iteration counter (the first output line should be “Spam 0”, the
second “Spam 1”, and so on).
6) Write a Python function that accepts 2 arguments, and returns their product. That is, the function should apply the “*” operator to the two objects passed in. Then, write calls to your function: the first call should pass in two numbers to invoke multiplication, and the second should trigger string repetition.
7) Write a Python function that accepts a list argument, and returns a new list that contains the first half of the items in the list passed in. For instance, given a list [1,2,3,4], your function should return [1,2]. Given a list [1,2,3,4,5], your function may return either [1,2], or [1,2,3]. Hints: recall the built-in “len” function and list slicing expressions.
8) Does the solution for question 7 work if you pass it a string or tuple? Why? What does it return in both cases?
9) Does the solution for question 7 work if you pass it a dictionary? Why? What does it return?
10) In the context of Python, the term “immutable” means:
11) Consider the three statements below: does the last one change the value of A?
A
= "spam"
B
= A
B
= "shrubbery"
12) Consider the three statements below: does the last one change the value of A?
A
= ["spam"]
B
= A
B[0] = "shrubbery"
13) If Python changes in a non-backward compatible way in the future, you should:
14) Extra credit: Why does the word “Spam” appear in so many Python examples?