Python Test Solution
Python Test Solution
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.
def foo_modifier(value):
value["foo"] = 25
bar = {}
print bar
foo_modifier(bar)
print bar
Output:-
{}
{“foo”: 2}
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.
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)
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?
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))