Skip to main content
Contents Index
Search Book
Search Results:
No results.
Readability settings Prev Up Next Scratch ActiveCode Profile
title here
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 5.39 Functions with Tuples and Dictionaries Multiple Choice Questions
Activity 5.39.1 .
Which of following are python dictionary methods?
keys() and get()
Correct! Both keys() and get() are dictionary methods.
values()
Correct, values() is a dictionary method.
count() and append()
Try again! These are list methods.
items()
Correct, items() is a dictionary method.
index()
Try again!
Activity 5.39.2 .
Which of the following would change
tup_transformation(90, 103, 54, 45) into
tup_transformation(5, 45, 54, 90)? (Note: Commas represent line breaks.)
def tup_transformation(tup):
# line(s)
tup.append(5), tup.pop(1), return sorted(tup)
Try again! Tuples are immutable.
tup.append(5), tup.pop(2), return sorted(tup)
Try again! Tuples are immutable.
tup.append(5), tup.pop(1), tup.reverse(), return tup
Try again! Tuples are immutable.
tup.append(5), tup.pop(1), return tup.reverse()
Try again! Tuples are immutable.
(90, 103, 54, 45) can’t be changed to (5, 45, 54, 90).
Correct! Tuples are immutable, so (90, 103, 54, 45) can’t be changed to (5, 45, 54, 90).
Activity 5.39.3 .
What would the output when
tup_and_list_transform((16, 7, 100, 0, 27),(84, 99, 78, 200, -7)) is called?
def tup_and_list_transform(tup1, tup2):
list_tup1 = list(tup1)
list_tup2 = list(tup2)
list_tup1.reverse()
return tuple(zip(list_tup1, list_tup2))
(0, 7, 16, 27, 100, 84, 99, 78, 200, -7)
Try again! Be careful not to sort in place of reverse. Also, by using zip, each tuple will have an element from list_tup1 and an element from list_tup2 in order.
(27, 0, 100, 7, 16, 84, 99, 78, 200, -7)
Try again! By using zip, each tuple will have an element from list_tup1 and an element from list_tup2 in order.
((0, 84), (7, 99), (16, 78), (27, 200), (100, -7))
Try again! Be careful not to sort in place of reverse.
((27, 84), (0, 99), (100, 78), (7, 200), (16, -7))
Correct! This converts the tuples to lists and reverses list_tup1 and zips list_tup1 and list_tup2 together.
The function call tup_and_list_transform((16, 7, 100, 0, 27), (84, 99, 78, 200, -7)) would cause an error because tuples are immutable.
Try again! While it’s true that tuples are immutable, tuples can be changed into data types that are mutable in order to be changed (e.g., lists).
Activity 5.39.4 .
Which of the following return statements will
not return a value for the function call
transform_dict({'one': 8, 'two': 10, 'three': 0})?
def transform_dict(dictionary):
# return statement goes here
return dictionary[1]
Correct! This will cause a key error since 1 is not a key.
return dictionary[’one’]
This will return 8.
return dictionary.get(’one’)
This will return 8.
return dictionary.get(8)
This will return None which is the default value to return from get if the key isn’t found.
return dictionary.get(’ten’, 5)
This will return 5 since ’ten’ is not a key.
Activity 5.39.5 .
Which of the following code snippets would cause
tup_of_tuples_to_dict((1, 'one'), (5, 'five')) to return
{1: 'one', 5: 'five'}?
def tup_of_tuples_to_dict(tup_of_tuples):
dictionary = {}
for tup in tup_of_tuples:
# insert code snippet here
return dictionary
dictionary[tup[0]] = dictionary[tup[1]]
Try again! There is no tup[1] value in the dictionary.
dictionary(tup[0]) = tup[1]
Try again! Setting up the key for a dictionary requires square brackets.
dictionary[tup[0]] = tup[1]
Correct! This will set the key to the first item in the tuple and the value to the second item.
dictionary[tup[1]] = tup[0]
Try again! This would set the key to the 2nd item and the value to the first.
dictionary[tup[0]] = dictionary.get(tup[1])
Try again! There is no tup[1] key in the dictionary, and the default value for the get() method is None.
Activity 5.39.6 .
What are the contents of
d after this code executes?
d = {3:4}
d[5] = d.get(4, 8)
d[4] = d.get(3, 9)
{3:4, 5:8, 4:9}
This would be true if the key 3 wasn’t in the dictionary, but it is.
{3:4, 5:8, 4:4}
It sets the value for key 4 to the value of key 3.
{3:4, 5:4, 4:3}
This would be true if get set the value to the first item rather than the second. The first item is the key to look for.
There will be an error
This code will run without any errors.
You have attempted
of
activities on this page.