Exercises
Contents
6.3. Exercises#
6.3.1. Exercise 1#
Given the following dictionary, create a list of its keys and check whether the list contains a view of the keys or a copy of the keys. Hint: Try adding/removing entries and check what happens with the list.
dictionary_of_cars = {'Ferrari': 1939, 'Mercedes-Benz': 1926,
'Volkswagen': 1937, 'Audi':1909,
'BMW': 1916}
# write your code here
6.3.2. Exercise 2#
Below you are given a list of names and a list of lists that contain the grades of some students. These lists are parallel. This means that the item at position 0 in the list_of_grades
corresponds to John
in the student
list and so on. Assuming that student names are unique, create a dictionary of student names and grades.
students = ['John', 'Joe', 'Alisson', 'Ann']
list_of_grades = [[4.5,5,4.75], [4.75, 5.25, 5.5],
[4, 4.25, 5.75], [5.5, 5.5, 5.75]]
# write your code here
6.3.3. Exercise 3#
Below you are given a list of names and a list of lists that contain the grades of some students. These lists are parallel just as in the previous exercise. Assuming that student names are unique, create a dictionary of student names and their corresponding Grade Point Average (GPA). Hint: Consider the zip() function.
students = ['John', 'Joe', 'Alisson', 'Ann']
list_of_grades = [[4.5,5,4.75], [4.75, 5.25, 5],
[4, 4.25, 6], [5.25, 5.5, 5.75]]
# write your code here
6.3.4. Exercise 4#
Find out if A is a superset of B using the issuperset()
method or the >
and >=
operators.
A = {1,2,3,4,5,6}
B = {2, 4, 6}
# write your code here
6.3.5. Exercise 5 (Optional)#
Can a frozenset contain sets as its elements? What about sets? Can they contain frozensets as elements?
# write your answer here as a comment
# write your code here if you want to test your reasoning
6.3.6. Exercise 6 (Optional)#
Given the text below, build a dictionary to map a word to the number of times it appears in the text. Then print only the characters that appear more than once. Also remove any empty character. Hint: Check the split() method. Source of text: “To be or not to be” soliloquy from Hamlet by Shakespeare.
text = 'To be, or not to be, that is the question: \
Whether tis nobler in the mind to suffer \
The slings and arrows of outrageous fortune, \
Or to take Arms against a Sea of troubles, \
And by opposing end them: to die, to sleep \
No more; and by a sleep, to say we end.'
# write your code here