Score of 12.00 out of 12.00.

Question 1

Consider this code:
>>> d = {'a': 1, 'b': 2}
>>> # CODE MISSING HERE
>>> d
{'a': 1, 'c': 3, 'b': 2}
Write the missing assignment statement that that modifies the dictionary as shown. (Just write the assignment statement; don't write the >>> part.)

You entered:

Your Answer
Score Explanation
d['c'] = 3 Correct 1.00
Total
1.00 / 1.00
Question ExplanationYou need to add 'c' as a key and 3 as the value associated with that key.

Question 2

Consider this code:
>>> d = {'a': 1, 'b': 2}
>>> # CODE MISSING HERE
>>> d
{'a': 1, 'b': 3}
Write the missing assignment statement that modifies the dictionary as shown. (Just write the assignment statement; don't write the >>> part.)

You entered:

Your Answer
Score Explanation
d['b'] = 3 Correct 1.00
Total
1.00 / 1.00
Question ExplanationYou need to replace the value associated with key 'b'.

Question 3

Consider this code:
>>> d = {'a': [1, 3], 'b': [5, 7]}

# CODE MISSING HERE

>>> d
{'a': [1, 2, 3], 'b': [5, 7]}
Select the option(s) that would lead to this result. Hint: call help on insert, append, and sort.
Your Answer
Score Explanation

d['a'].insert(2, 1)
Correct 0.25 Look up the help for method insert. In which order are the parameters?

d['a'].insert(1, 2)
Correct 0.25

d['a'].append(2)
d['a'].sort()
Correct 0.25

d['a'].append(2)
Correct 0.25 This doesn't put the 2 between the 1 and the 3.
Total
1.00 / 1.00
Question ExplanationWhat does d['a'] evaluate to?
What happens when you append an item to a list and then sort that list?

Question 4

Consider this assignment statement:
d = {'a': 1, 'c': 3, 'b': 2}
Select the expression(s) that evaluate to True.
Your Answer
Score Explanation
"b" in d Correct 0.25
'b' in d Correct 0.25
2 in d Correct 0.25
'B' in d Correct 0.25
Total
1.00 / 1.00
Question ExplanationPython treats single- and double-quoted strings as being equivalent.
Operator in checks whether something is a key in a dictionary. It does not check whether something is a value in a dictionary.

Question 5

Consider this code:
d = {'a': [1, 3], 'b': [5, 7, 9], 'c': [11]}
Select the expression(s) that evaluate to 3.
Your Answer
Score Explanation
len(d['b']) Correct 0.25
len(d) Correct 0.25
len(d['a']) Correct 0.25
len(d['a' + 'c']) Correct 0.25
Total
1.00 / 1.00
Question Explanationlen works on dictionaries, lists, and strings. When a dictionary is passed as an argument, it returns the number of key/value pairs in that dictionary.
Each value in d is a list, and so len can be called on those lists.

Question 6

Consider this code:
tup = (1, 2, 3)
Select the expression(s) and statement(s) below that result in an error.
Your Answer
Score Explanation
subtup = tup[0:2] Correct 0.25
tup[0:2] == (10, 30) Correct 0.25
tup[-2] = 4 Correct 0.25
tup.reverse() Correct 0.25
Total
1.00 / 1.00
Question ExplanationTuples are immutable, so any code that tries to change them results in an error.

Question 7

Select the expression(s) that can be used as dictionary keys.
Your Answer
Score Explanation
('single',) Correct 0.25
['a', 'b'] Correct 0.25
{1: 2, 3: 4} Correct 0.25 Dictionaries can't be used as keys.
(1, 'fred', 2.0) Correct 0.25
Total
1.00 / 1.00
Question ExplanationKeys must be immutable, so lists and dictionaries are not allowed as keys.

Question 8

Consider this code:
d = {1: ['a', 'b', 'c'], 2: ['d', 'e'], 3: []}
Select the code fragment(s) that set variable total to the number of items in all the lists that occur as values in d.
Your Answer
Score Explanation

L = []
for k in d:
    L.extend(d[k])

total = len(L)
Correct 0.25

L = []
for k in d:
    L.append(k)

total = len(L)
Correct 0.25

total = 0
for k in d:
    total = total + k
Correct 0.25

total = 0
for k in d:
    total = total + len(d[k])
Correct 0.25
Total
1.00 / 1.00
Question ExplanationEach value in d is a list of 0 or more elements. Choose the options that count the total number of these elements in d (5 in the above example).

Question 9

This dictionary has 3 keys that are all the same. Enter this in the Python shell:
{1: 10, 1: 20, 1: 30}
Submit what the code above evaluates to; don't submit your answers to the thought questions below.
What we want you to think about: We haven't covered this situation in the videos; what do you think should happen? Do you think this should this cause an error? Should it discard some of the key/value pairs? If so, which one do you think it should keep? People who create programming languages have to make these kinds of decisions, and often there isn't a clear good choice.

You entered:

Your Answer
Score Explanation
{1: 30} Correct 1.00
Total
1.00 / 1.00
Question ExplanationRun the code in the Python shell and enter what it evaluates to. No need to enter your answers to the thought questions, just what results from evaluating the line of code.

Question 10

Consider this code:
L = [['apple', 3], ['pear', 2], ['banana', 3]]
d = {}
for item in L:
    d[item[0]] = item[1]
What does this code do?
Your Answer
Score Explanation
Populates dictionary L where each key is the first item of each inner list of d and each value is the second item of that inner list.


Reorders the items in the inner lists of L.


Removes the items from L and populates dictionary d where each key is the first item of each inner list of L and each value is the second item of that inner list.


Populates dictionary d where each key is the first item of each inner list of L and each value is the second item of that inner list. Correct 1.00
Total
1.00 / 1.00
Question ExplanationRun the code and examine the values of d and L.

Question 11

Consider this code:
def eat(d):
    '''(dict of {str: int}) -> bool

    Each key in d is a fruit and each value is the quantity of that fruit.

    REST OF DESCRIPTION MISSING HERE

    >>> eat({'apple': 2, 'banana': 3, 'pear': 3, 'peach': 1})
    True
    >>> eat({'apple': 0, 'banana': 0})
    False
    '''
    ate = False
    for fruit in d:
        if d[fruit] > 0:
            d[fruit] = d[fruit] - 1
            ate = True

    return ate
Select the most appropriate description below.
Your Answer
Score

Return True if and only if any fruit was eaten.




Remove from d all fruits that have a value of 0 associated with them and
return True if and only if the were no such fruits.




Reduce by 1 all quantities greater than 0 associated with each fruit in d.




Try to eat one of each fruit: reduce by 1 all quantities greater than 0
associated with each fruit in d and return True if and only if any fruit was
eaten.
Correct 1.00
Total
1.00 / 1.00
Question ExplanationDocstrings should describe side-effects and return values.

Question 12

Consider the code:
def contains(v, d):
    ''' (object, dict of {object: list}) -> bool

    Return whether v is an element of one of the list values in d.

    >>> contains('moogah', {1: [70, 'blue'], 2: [1.24, 'moogah', 90], 3.14: [80, 100]})
    True
    >>> contains('moogah', {'moogah': [1.24, 'frooble', 90], 3.14: [80, 100]})
    False
    '''

    found = False  # Whether we have found v in a list in d.

    # CODE MISSING HERE

    return found
Select the code fragment(s) that make the function above match its docstring description.
Your Answer
Score Explanation

    for k in d:
        for i in range(len(d[k])):
            if d[k][i] == v:
                found = True
Correct 0.25

    for k in d:
        for i in range(len(d[k])):
            found = (d[k][i] == v)
Correct 0.25

    for k in d:
        if v == k:
            found = True
Correct 0.25

    for k in d:
        if v in d[k]:
            found = True
Correct 0.25
Total
1.00 / 1.00
Question ExplanationWhich code fragment(s) set found to be True if and only if v is an element of one of the lists that are values of dictionary d?