10to8 Python Test
For the following questions, you can use a Python interpreter to check your answers. But
please do not seek help from other people. The test is designed to gauge your experience
with Python, so do not worry if you do not have lots of experience and struggle with the later
questions.
We expect the test should take 1 hour at most.
1. What does this print?
def foo_modifier(value):
value["foo"] = 25
bar = {}
print bar
foo_modifier(bar)
print bar
Output:-
{}
{“foo”: 2}
2. What does this print?
def baz_modifier(baz):
baz = 42
baz = 21
print baz
baz_modifier(baz)
print baz
Output:-
21
21
3. The code in Q1 and Q2 look very similar to each other, but give different results. Why is
this? Why does the code in Q2 not print 42?
Output:-
Because object or dictionary are reference types which is mutable in nature, for
dictionary Python will not create a new object it will use the same dictionary and
number is primitive types which is immutable in nature, for number Python will create
a new object.
4. What are list comprehensions? Can you write a list comprehension that creates all the
even numbers from 0 to 100 inclusive?
Output:-
List comprehension offers a shorter syntax when you want to create a new list based
on the values of an existing list.
even_nos_from_0_to_100 = [num for num in range(0, 101) if num % 2 == 0]
print(even_nos_from_0_to_100)
5. What does the following print:
array = [ 1, 2, 3]
print array[-1]
print array[:-2]
Output:-
3
[1]
6. How might you download a webpage, and identify/parse all the <h1> tags in it?
Output:-
from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen('http://www.example.com/')
bsh = BeautifulSoup(html.read(), 'html.parser')
print(bsh.h1)
7. What does the python "with" statement do?
Output:-
with statement in Python is used in exception handling to make the code cleaner and
much more readable. It simplifies the management of common resources like file
streams.
8. What does the following print? Do the comments match what is printed? Why are the
comments different to what is printed?
How would you change the c ode for the function extend_list (not the way that the
function is called) so that the printed output matches the comments?
def extend_list(new_value, list=[]):
list.append(new_value)
return list
first_list = extend_list(1)
first_list = extend_list(2, first_list)
# Should print [ 1, 2 ]
print first_list
second_list = extend_list("a")
second_list = extend_list("b", second_list)
# Should print [ "a", "b" ]
print second_list
Output:-
def extend_list(new_value, list=None):
if list is None:
list = []
list.append(new_value)
return list
first_list = extend_list(1)
first_list = extend_list(2, first_list)
# Should print [ 1, 2 ]
print(first_list)
second_list = extend_list("a")
second_list = extend_list("b", second_list)
# Should print [ "a", "b" ]
print(second_list)
# [1, 2]
# [“a”, “b”]
9. Write a function to print the values of all the leaf nodes (A, B etc)
data = {
"A": 6,
"Foo": {
"Bar": {
"B": 4,
...
},
...
},
"Foo2": {
"Bar2": {
"Baz": {
"C": 25,
...
}
},
...
}
}
def print_leaves_on_tree(data):
# Your code here...
# ... print A: 6, B: 4, C: 25 ...
Output:-
res = []
def print_leaves_on_tree(data):
# Your code here...
# ... print A: 6, B: 4, C: 25 ...
for key, value in data.items():
if (type(value) == dict):
print_leaves_on_tree(value)
else:
# print('{}: {}'.format(key, value))
res.append('{}: {}'.format(key, value))
return res
output = print_leaves_on_tree(data)
print(", ".join(output))