Dictionaries and Sets

Difference Between List, Tuple, Set and Dictionary in Python - Kindson The  Genius

Dictionaries and Sets

  1. (20 points, each 2 points) True or False questions:
  1. (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:

  1. (True/False) Set is mutable, set elements must be immutable.

Answer:

  • (30 points, each 3 points) Multiple choice or Fill in blank questions:
  1. 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

  1. It updates the value associated with the key, replacing the original value
    1. It compares the value with the key
    1. 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'}
  1. dictionary
  2. set
  3. list
  4. 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}

  1. print(roman_numerals)
    1. print(list(roman_numerals.keys()))
    1. print(list(roman_numerals.values()))
    1. 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 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

MonTueWedThu
66507567
70568074
74648381
    

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

Unlike most other websites we deliver what we promise;

  • Our Support Staff are online 24/7
  • Our Writers are available 24/7
  • Most Urgent order is delivered with 6 Hrs
  • 100% Original Assignment Plagiarism report can be sent to you upon request.

GET 15 % DISCOUNT TODAY use the discount code PAPER15 at the order form.

Type of paper Academic level Subject area
Number of pages Paper urgency Cost per page:
 Total: