Dictionaries and Sets
- (20 points, each 2 points) True or False questions:
- (True/False) Dictionary keys must be unique. However, multiple keys may have the same value.
Answer:
- (True/False) A view does not have its own copy of the corresponding data from the dictionary. As the dictionary changes, each view changes dynamically too. For example, the view object returned by dict.keys(), dict.values(), dict.items().
Answer:
3)(True/False) Dictionary method keys() returns an ordered list of the dictionary’s keys.
Answer:
- (True/False) The = = comparison evaluates to True only if both dictionaries have the same key-value pairs in the same order
Answer:
- (True/False) Dictionary copy() method returns a deep copy of the dictionary.
Answer:
- (True/False) Sets are collections of unique mutable and immutable objects.
Answer:
- (True/False) Set method update() actually performs a union operation, modifying the set on which it’s called.
Answer:
- (True/False) Two sets are disjoint if they do not have any common elements.
Answer:
- (True/False) You can create a set from another collection of values by using the built-in set() function.
Answer:
- (True/False) Set is mutable, set elements must be immutable.
Answer:
- (30 points, each 3 points) Multiple choice or Fill in blank questions:
- Dictionaries can be thought of as unordered collection in which each value is access through its corresponding _______.
a) key
b) position
Answer:
- Dictionary method _______ returns each key-value pair as a tuple.
Answer:
- myVar = {}, myVar is an empty ____
- dictionary
- set
- list
- tuple
Answer:
- What does an expression of the following form do when the key is in the dictionary?
dictionaryName[key] = value
- It updates the value associated with the key, replacing the original value
- It compares the value with the key
- It finds the value associated with the key
Answer:
- Two sets are _____ if the sets do not have any common elements
Answer:
6) What is the data type of {3, 7, 'one', 'two'}
- dictionary
- set
- list
- tuple
Answer:
- (4 points) set() create an empty ___, ( ) create an empty ___
- set
- tuple
- list
- dict
Answer:
- (4 points)What’re the results for these operations:
- {10, 20, 30} – {5, 10, 15, 20}
- {10, 20, 30} ^ {5, 10, 15, 20}
- {10, 20, 30} | {5, 10, 15, 20}
- {10, 20, 30} & {5, 10, 15, 20}
Answer:
- (4 points) What are the print results:
roman_numerals = {‘I’: 1, ‘II’: 2, ‘III’: 3, ‘V’: 5}
- print(roman_numerals)
- print(list(roman_numerals.keys()))
- print(list(roman_numerals.values()))
- print(list(roman_numerals.items()))
Answer:
- (20 points) Hand-trace the following code. What is the output, or what error/problem do you observe and why?
1) (4 points)
country_codes = {‘Finland’: ‘fi’, ‘South Africa’: ‘za’, ‘Nepal’: ‘np’}print(country_codes) |
Output: |
2) (4 points)
grades = {‘Sue’:[98, 90, 85], ‘Bob’:[84, 95, 91]}for k in grades: print(k) |
Output: |
3) (4 points)
ori = {1: ‘apple’, 2: ‘orange’ }new = ori.copy()new.clear()print(‘new: ’, new)print(‘ori: ’, ori) |
Output: |
4) (4 points)
mySet = set([1, 2, 3])mySet.update([2, 4, 7]]print(mySet) |
Output: |
5) (4 points)
num = list(range(3)) + list(range(5))mySet = set(num)print(mySet) |
Output: |
4. (15 points) Write a program:
- The following table contains temperatures from Mon through Thu
- Use dictionary data structure to hold the data in this table
- Calculate the average temperature for each day
- Print the output in the following format:
Hint: refer to Dictionary Comprehensions on slide page 32
Mon | Tue | Wed | Thu |
66 | 50 | 75 | 67 |
70 | 56 | 80 | 74 |
74 | 64 | 83 | 81 |
Output:
- (12 points) Write your program here, or copy/paste a screenshot of your Program:
- (3 points) Save the program as “table.py”. Upload the .py file as part of your submission.
- (15 points) (Duplicate word removal) Write a function that reads in a text and determines the number of unique words in alphabetical order.
- Read in a text, for example “Let it be let it be”, use string.split(separator) function to break it into a list of strings/words by the specified separator (white space).
- Displays in alphabetical order only the unique words. Treat uppercase and lowercase letters the same (as lowercase).
- Demonstrate how to use string.split(separator) ( If separator not provided, then any white space is a separator).
text = ‘Let it be let it be’
myList = text.split( ) # myList is a list: [‘Let’, ‘it’, ‘be’, ‘let’, ‘it’, ‘be’]
- The function should use a set to get the unique words in the list.
- Print out the unique words and the number of unique words
- Test your function with other text/sentences
- (10 points) Write your program here, or copy/paste a screenshot of your Program:
- (2 points) copy/paste Program Run:
- (3 points) Save the program as “removeDuplicateWords.py”. Upload the .py file as part of your submission.
6.Use a dictionary comprehension to create a dictionary of the numbers 1–5 and their cubes:
- (8 points) Write the program here, or copy/paste the screenshot of your program
- (2 point) Save the program as “dictCubes.py”. Upload the .py file as part of your submission.
Want help to write your Essay or Assignments? Click here