Learning Python (original) (raw)
Learning Python
Final Exam
Student name:
1) Python was created by (circle one):
- Bill Gates
- A committee of open source users
- A Dutchman named Guido
- Al Gore
2) To traverse a directory tree in Python, you would be most likely to use:
- Tkinter
- The os.walk call
- The socket module
- Hiking boots
- Regular expressions
3) Python is useful for:
- Internet scripting
- Systems programming
- Graphical user interfaces
- Component integration
- Database access
- Text processing
- Numeric programming
- All of the above
- 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).
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.
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.
Does the solution for question 7 work if you pass it a string or tuple?� Why?� What does it return in both cases?
Does the solution for question 7 work if you pass it a dictionary?� Why?� What does it return?
In the context of Python, the term �immutable� means:
- Doesn�t talk much
- Cannot be changed in future Python releases
- Cannot be changed in-place
- Unreasonably stubborn objects
- Consider the three statements below: does the last one change the value of A?
A = "spam"
B = A
B = "shrubbery"
- Consider the three statements below: does the last one change the value of A?
A = ["spam"]
B = A
B[0] = "shrubbery"
- If Python changes in a non-backward compatible way in the future, you should:
- Call Guido at home
- Seek spiritual enlightenment
- Change careers
- Mail python-dev@python.org
- Extra credit: Why does the word �Spam� appear in so many Python examples?