Py4e: Chapter 9- Questions and Answers
|100% Verified
What would the following Python code print out?
stuff = dict()
print(stuff['candy']) Correct Ans-The program would fail with a traceback notcandy
What would the following Python code print out?
stuff = dict()
print(stuff.get('candy',-1)) Correct Ans--1 not The program would fail with a traceback
(T/F) When you add items to a dictionary they remain in the order in which you added
them. Correct Ans-False
What is a common use of Python dictionaries in a program? Correct Ans-Building a histogram
counting the occurrences of various strings in a file
Which of the following lines of Python is equivalent to the following sequence of statements
assuming that counts is a dictionary?
if key in counts:
counts[key] = counts[key] + 1
else:
counts[key] = 1 Correct Ans-counts[key] = counts.get(key,0) + 1
|100% Verified
What would the following Python code print out?
stuff = dict()
print(stuff['candy']) Correct Ans-The program would fail with a traceback notcandy
What would the following Python code print out?
stuff = dict()
print(stuff.get('candy',-1)) Correct Ans--1 not The program would fail with a traceback
(T/F) When you add items to a dictionary they remain in the order in which you added
them. Correct Ans-False
What is a common use of Python dictionaries in a program? Correct Ans-Building a histogram
counting the occurrences of various strings in a file
Which of the following lines of Python is equivalent to the following sequence of statements
assuming that counts is a dictionary?
if key in counts:
counts[key] = counts[key] + 1
else:
counts[key] = 1 Correct Ans-counts[key] = counts.get(key,0) + 1