Exercise 2 Solutions¶
Question 1¶
matches = [0.6, 0.8, 0.94, 0.2, 0.4]
good_matches = []
for m in matches:
if m > 0.7:
good_matches += [m]
good_matches[0.8, 0.94][0.7] + 0.8---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[2], line 1
----> 1 [0.7] + 0.8
TypeError: can only concatenate list (not "float") to listQuestion 2¶
sample_matches = [
['SAM1', 0.6],
['SAM2', 0.8],
['SAM3', 0.94],
['SAM4', 0.2],
['SAM5', 0.4]
]
good_samples = []
for s in sample_matches:
print(s[0], type(s[0]))
if s[1] > 0.7:
good_samples += [s[0]]
good_samplesSAM1 <class 'str'>
SAM2 <class 'str'>
SAM3 <class 'str'>
SAM4 <class 'str'>
SAM5 <class 'str'>
['SAM2', 'SAM3']s = ['SAM1', 0.6]
s[0]'SAM1'sample_matches = [
['SAM1', 0.6],
['SAM2', 0.8],
['SAM3', 0.94],
['SAM4', 0.2],
['SAM5', 0.4]
]
good_samples = []
for sample_name, match in sample_matches:
if match > 0.7:
good_samples += [sample_name]
good_samples['SAM2', 'SAM3']for i, val in enumerate(sample_matches):
print(i)
print(val)0
['SAM1', 0.6]
1
['SAM2', 0.8]
2
['SAM3', 0.94]
3
['SAM4', 0.2]
4
['SAM5', 0.4]
for element in enumerate(sample_matches):
print(element[0])
print(element[1])0
['SAM1', 0.6]
1
['SAM2', 0.8]
2
['SAM3', 0.94]
3
['SAM4', 0.2]
4
['SAM5', 0.4]
(a, b) = (1, 5)
b5my_tuple = (1,2,3)my_tuple[0]1my_tuple(0)---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[11], line 1
----> 1 my_tuple(0)
TypeError: 'tuple' object is not callable[('SAM1', 0.8),]
('SAM1', 0.8, 'SAM2', 0.9)('SAM1', 0.8, 'SAM2', 0.9)Question 3¶
def find_good_matches(threshold, match_list):
good_samples = []
for sample_name, match in match_list:
if match > threshold:
good_samples += [(sample_name, match)]
return good_samplesfind_good_matches(0.7, sample_matches)[('SAM2', 0.8), ('SAM3', 0.94)]def square_it(n):
return n * nval = square_it(2)print("val is:", val)val is: 4
def square_it2(n):
print(n * n)val = square_it2(2)4
print(type(val))<class 'NoneType'>
def find_good_matches(threshold, good_match_list):
"""Does something useful"""
tuple_list = ()
for s, m in good_match_list:
if m > threshold:
tuple_list += ((s, m),)
return tuple_listsample_matches = [
['SAM1', 0.6],
['SAM2', 0.8],
['SAM3', 0.94],
['SAM4', 0.2],
['SAM5', 0.4]
]
threshold = 0.7
find_good_matches(threshold, sample_matches)(('SAM2', 0.8), ('SAM3', 0.94))Question 4¶
Write a function sum_it(a, b) that takes two numbers, a and b and returns the result of adding them together. Then write a function print_sum(a, b) that takes two numbers, a and b and prints the result of adding the two numbers together. What is the difference between these two functions? Which one returns a value?
def sum_it(a, b):
return a + b
def print_sum(a, b):
print(a + b)the_sum = sum_it(5, 7)
print("the_sum", the_sum)
the_next_sum = sum_it(the_sum, 3)the_sum 12
print("the_next_sum", the_next_sum)the_next_sum 15
the_sum = print_sum(5, 7)12
the_sumthe_sum = print_sum(5, 7)
print("the_sum", the_sum)
the_next_sum = print_sum(the_sum, 3)12
the_sum None
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[29], line 3
1 the_sum = print_sum(5, 7)
2 print("the_sum", the_sum)
----> 3 the_next_sum = print_sum(the_sum, 3)
Cell In[24], line 2, in print_sum(a, b)
1 def print_sum(a, b):
----> 2 print(a + b)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'Question 5¶
Write a function double_it that takes a list of numbers and doubles each of them in place.
my_list = [1, 2, 3]
my_list[0] = my_list[0] * 2
my_list[1] = my_list[1] * 2
my_list[2] = my_list[2] * 2
my_list[2, 4, 6]for num in my_list:
num * 2range(len(my_list))
enumerate(my_list)
# enumerate is an iterator
# so we can do
next(enumerate(my_list))(0, 2)print("my_list", my_list)
# get all of the values from the enumerate iterator all at once
list(enumerate(my_list))my_list [2, 4, 6]
[(0, 2), (1, 4), (2, 6)]my_students = ['Jean-Carl', 'Sahana', 'Siphemelele', 'Sohail']
print("my_students", my_students)
print(list(enumerate(my_students)))my_students ['Jean-Carl', 'Sahana', 'Siphemelele', 'Sohail']
[(0, 'Jean-Carl'), (1, 'Sahana'), (2, 'Siphemelele'), (3, 'Sohail')]
int_val, string_val = (0, 'Ghost')
print("int_val", int_val)
print("string_val", string_val)int_val 0
string_val Ghost
val1, val2 = (1, 2, 3)---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[36], line 1
----> 1 val1, val2 = (1, 2, 3)
ValueError: too many values to unpack (expected 2, got 3)my_students = ['Jean-Carl', 'Sahana', 'Siphemelele', 'Sohail']
print("my_students", my_students)
for i, my_student in enumerate(my_students):
new_string = 'Student ' + my_student
my_students[i] = new_string
print("my_students now", my_students)my_students ['Jean-Carl', 'Sahana', 'Siphemelele', 'Sohail']
my_students now ['Student Jean-Carl', 'Student Sahana', 'Student Siphemelele', 'Student Sohail']
my_list = [2, 4, 6]
for i, num in my_list:
print(i, num)---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[38], line 2
1 my_list = [2, 4, 6]
----> 2 for i, num in my_list:
3 print(i, num)
TypeError: cannot unpack non-iterable int objectmy_tuple_list = [(1, 2), (3, 4)]
for i, num in my_tuple_list:
print(i, num)1 2
3 4
my_tuple_list = [(1, 2), (3, 4), 5]
for i, num in my_tuple_list:
print(i, num)1 2
3 4
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[40], line 2
1 my_tuple_list = [(1, 2), (3, 4), 5]
----> 2 for i, num in my_tuple_list:
3 print(i, num)
TypeError: cannot unpack non-iterable int objectmy_students = ['Jean-Carl', 'Sahana', 'Siphemelele', 'Sohail']
print("my_students", my_students)
name_lengths = []
len(my_students[0])
# how would I print the length of each name in the list
my_students ['Jean-Carl', 'Sahana', 'Siphemelele', 'Sohail']
9my_students = ['Jean-Carl', 'Sahana', 'Siphemelele', 'Sohail']
for name in my_students:
print(len(name))9
6
11
6
# print the index of the student whose name is Sahana
my_students = ['Jean-Carl', 'Sahana', 'Siphemelele', 'Sohail']
# things to use
# for loop
# enumerate
# name == "Sahana"
# if statement
my_students = ['Jean-Carl', 'Sahana', 'Siphemelele', 'Sohail']
for index, value in enumerate(my_students):
if value == "Sahana":
print(index)1
my_students = ['Jean-Carl', 'Sahana', 'Siphemelele', 'Sohail']
for index, name in enumerate(my_students):
if name == "Sohail":
print(index)3
my_students = ['Jean-Carl', 'Sahana', 'Siphemelele', 'Sohail']
print("my_students", my_students)
for index, value in enumerate(my_students):
new_string = 'Student ' + value
my_students[index] = new_string
print("my_students now", my_students)my_students ['Jean-Carl', 'Sahana', 'Siphemelele', 'Sohail']
my_students now ['Student Jean-Carl', 'Student Sahana', 'Student Siphemelele', 'Student Sohail']
my_students = ['Jean-Carl', 'Sahana', 'Siphemelele', 'Sohail']
print("my_students", my_students)
new_student_list = []
for index, value in enumerate(my_students):
new_string = 'Student ' + value
new_student_list += [new_string]
print("my_students now", my_students)
print("new_student_list", new_student_list)my_students ['Jean-Carl', 'Sahana', 'Siphemelele', 'Sohail']
my_students now ['Jean-Carl', 'Sahana', 'Siphemelele', 'Sohail']
new_student_list ['Student Jean-Carl', 'Student Sahana', 'Student Siphemelele', 'Student Sohail']
my_students = ['Jean-Carl', 'Sahana', 'Siphemelele', 'Sohail']
print("my_students", my_students)
for index, value in enumerate(my_students):
new_string = 'Student ' + value
my_students[2] = new_string
print("index", index, "my_students now", my_students)
print("my_students now", my_students)my_students ['Jean-Carl', 'Sahana', 'Siphemelele', 'Sohail']
index 0 my_students now ['Jean-Carl', 'Sahana', 'Student Jean-Carl', 'Sohail']
index 1 my_students now ['Jean-Carl', 'Sahana', 'Student Sahana', 'Sohail']
index 2 my_students now ['Jean-Carl', 'Sahana', 'Student Student Sahana', 'Sohail']
index 3 my_students now ['Jean-Carl', 'Sahana', 'Student Sohail', 'Sohail']
my_students now ['Jean-Carl', 'Sahana', 'Student Sohail', 'Sohail']
# this answer gives you clues for question 5
my_students = ['Jean-Carl', 'Sahana', 'Siphemelele', 'Sohail']
print("my_students", my_students)
def update_students(student_list):
for index, value in enumerate(student_list):
new_string = 'Student ' + value
student_list[index] = new_string
new_list = update_students(my_students)my_students ['Jean-Carl', 'Sahana', 'Siphemelele', 'Sohail']