A program stores election results in a Python dictionary, looking each result up by region code and year. Which of these values cannot be used as a key, because Python raises a TypeError when you try to store it?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A Python program stores a dictionary prices that maps item names (strings) to prices (floats). Complete each statement with the name of the dictionary method, written without the dot or the parentheses.

  • The method copies every key-value pair from another dictionary into prices, overwriting the value of any key that is already present.
  • The method returns a view of key-value pairs, so a for loop can unpack each name and its price together.
  • The method returns the value for a key if the key exists; otherwise it inserts the key with a supplied default value and returns that default.
  • The method returns a view of the prices alone, which can be passed straight to sum to total them.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A student tallies colour votes with the Python code below. What number does the final line print? Give your answer as a whole number.


Python
words = ["red", "blue", "red", "green", "blue", "red"]
counts = {}
for w in words:
    counts[w] = counts.get(w, 0) + 1
print(counts["red"] * len(counts))

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors