MAT 215 Chapter 6

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

6.2 Q6: Which of the following statements a), b) or c) is false? a. Assigning a value to a nonexistent key inserts the key-value pair in the dictionary. b. String keys are case insensitive. c. Assigning a value to a nonexistent key could be a logic error. d. All of the above statements are true.

Answer: b. Actually, string keys are case sensitive, so 'x' and 'X' are different keys.

What values are actually displayed where we've inserted ??? in Out[1] and Out[2]: In [1]: {1, 3, 5}.isdisjoint({2, 4, 6}) Out[1]: ??? In [2]: {1, 3, 5}.isdisjoint({4, 6, 1}) Out[2]: ??? a. False, False b. False, True c. True, False d. True, True

Answer: c

Which of the following statements a), b) or c) is false? a. Sets are immutable, so sets can have other sets as elements. b. A frozenset is an immutable set—it cannot be modified after you create it, so a set can contain frozensets as elements. c. The built-in function frozenset creates a frozenset from any iterable. d. All of the above statements are true.

Answer: a. Actually, sets are mutable—you can add and remove elements, but set elements must be immutable. Therefore, a set cannot have other sets as elements.

Which of the following statements a), b) or c) is false? a. Like operator |, union augmented assignment |= performs a set union operation, but |= doesn't modify its left operand. b. The set type's update method performs a union operation modifying the set on which it's called—the argument can be any iterable. c. Other mutable set methods are intersection augmented assignment &=, difference augmented assignment -= and symmetric difference augmented assignment ^=. Their corresponding methods with iterable arguments are intersection_update, difference_update and symmetric_difference_update. d. All of the above statements are true.

Answer: a. Actually, |= does

Which of the following statements a), b) or c) is false? a. A dictionary associates keys with values. Each key maps to a specific value. b. A dictionary's keys must be mutable (such as strings, numbers or tuples) and unique (that is, no duplicates). c. Multiple keys can have the same value, such as two different inventory codes that have the same quantity in stock. d. All of the above statements are true.

Answer: b. Actually, a dictionary's keys must be immutable (such as strings, numbers or tuples) and unique (that is, no duplicates)

Which of the following statements is false? a. Lists and tuples are built-in sequence collections. b. Dictionaries, strings and sets are built-in non-sequence collections. c. A dictionary is an unordered collection which stores key-value pairs that map immutable keys to values, just as a conventional dictionary maps words to definitions. d. A set is an unordered collection of unique immutable elements.

Answer: b. Actually, dictionaries and sets are built-in non-sequence collections, but strings are built-in sequence collections

Which of the following statements a), b) or c) is false? a. For each animation frame, a Matplotlib FuncAnimation calls a function that you define to specify how to change the plot. b. Displaying animation frames to the screen is relatively fast compared to the die rolls, which occur at the computer's CPU speeds. c. If we roll only one die per animation frame, we won't be able to run a large number of rolls in a reasonable amount of time. You can increase the execution speed of the simulation by rolling the die more times per animation frame. d. All of the above statements are true.

Answer: b. Actually, displaying animation frames to the screen is a relatively slow input/output- bound operation compared to the die rolls, which occur at the computer's super-fast CPU speed

terating through a Dictionary 6.2 Q4: Which of the following statements a), b) and c) is false? a. The following dictionary maps month-name strings to int values representing the numbers of days in the corresponding month: days_per_month = {'January' : 31 , 'February' : 28 , 'March' : 31 } b. Multiple keys in a dictionary can have the same value. c. The following for statement iterates through dictionary days_per_month's key-value pairs. Dictionary method items returns each key-value pair as a tuple, which is unpacked into the target variables month and days: for month, days in days_per_month.items(): print(f' {month} has {days} days' ) d. All of the above statements are true.

Answer: b. Actually, multiple values in a dictionary can have the same value. Keys must be unique

onsider the following session: In [1]: numbers = list(range(10)) + list(range(5)) In [2]: numbers Out[2]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4] In [3]: set(numbers) Out[3]: ??? Which of the following would be displayed where we wrote ??? in Out[3]? a. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] b. (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) c. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} d. {[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]} .

Answer: c

Which of the following statements a), b) or c) is false? a. Dictionary comprehensions provide a convenient notation for quickly generating dictionaries, often by mapping one dictionary to another. b. The following code, which has a dictionary with unique values, reverses the key-value pairs, so the values become the keys and vice versa: months = {'January': 1, 'February': 2, 'March': 3} months2 = {number: name for name, number in months.items()} c. If the values in Part (b) were not unique, then the code would generate a ValueError. d. All of the above statements are true.

Answer: c. Actually, it's not an error. The non-unique values would become non-unique keys.That's not allowed, but it's not a problem because each non-unique key-value pair would simplyupdate that key's value in the dictionary to the new value. Thus, the new dictionary would havefewer key-value pairs.

Which of the following statements is false? a. You can determine the number of items in a set with the built-in len function. b. You can check whether a set contains a particular value using the in and not in operators. c. Sets are not iterable, so you cannot process each set element with a for loop. d. Sets are unordered, so there's no significance to the iteration order.

Answer: c. Actually, sets are iterable, so you can process each set element with a for loop.

Which of the following statements a), b) or c) is false? a. The following code creates a set of strings named colors: colors = {'red', 'orange', 'yellow', 'green', 'red', 'blue'} b. Duplicate elimination is automatic when creating a set. c. Sets are ordered, so you can write code that depends on the order of their elements. d. All of the above statements are true.

Answer: c. Actually, sets are unordered, so you should not write code that depends on the order of their elements

Which of the following statements is false? a. You can create a dictionary by enclosing in curly braces, {}, a comma-separated list of key-value pairs, each of the form key: value. b. You can create an empty dictionary with {}. c. The following statement creates a dictionary with the country-name keys 'Finland', 'South Africa' and 'Nepal' and their corresponding Internet country code values 'fi', 'za' and 'np': country_codes = {'Finland', 'fi', 'South Africa', 'za', 'Nepal', 'np'} d. When you output a dictionary, its comma-separated list of key-value pairs is always enclosed in curly braces.

Answer: c. Actually, the following code creates a dictionary with the country-name keys 'Finland', 'South Africa' and 'Nepal' and their corresponding Internet country code values 'fi', 'za' and 'np': country_codes = {'Finland': 'fi', 'South Africa': 'za', 'Nepal': 'np'}

Which of the following statements a), b) or c) is false? a. The comparison operators == and != can be used to determine whether two dictionaries have identical or different contents. b. An equals (==) comparison evaluates to True if both dictionaries have the same key-value pairs, regardless of the order in which those pairs were added to each dictionary. c. In the following dictionary, the keys are lists of three integer student grades and the values are strings representing the students' names. grade_book = { 'Susan': [92, 85, 100], 'Eduardo': [83, 95, 79], 'Azizi': [91, 89, 82], 'Pantipa': [97, 91, 92] d. All of the above statements are true.

Answer: c. Actually, the keys are the student name strings and the values are the lists of three integer grades per studen

Which of the following statements is false? a. The union of two sets is a set consisting of all the unique elements from both sets. b. You can calculate the union with the | operator or with the set type's union method. c. The operands of the binary set operators, like |, must both be any iterable. d. The set methods may receive any iterable object as an argument.

Answer: c. Actually, the operands of the binary set operators, like |, must both be sets.

Which of the following statements a), b) or c) is false? a. If the random module's randrange function indeed produces integers at random, then every number in the specified range has an equal probability (or likelihood) of being chosen each time the function is called. b. For six-sided die rolls, each value 1 through 6 should occur about one-sixth of the time, so the probability of any one of these values occurring is 1/6th or about 16.667%. c. The more die rolls we attempt, the closer each die value's percentage of the total rolls gets to 16.667%. This is a manifestation of Moore's Law. d. All of the above statements are true.

Answer: c. Actually, this is a manifestation of the Law of Large Numbers.

Which of the following statements is false? a. A set is an unordered collection of unique values. b. Sets may contain only immutable objects, like strings, ints, floats and tuples that contain only immutable elements. c. Sets are iterable, so they are sequences and they support indexing and slicing with square brackets, []. d. Dictionaries do not support slicing.

Answer: c. Actually, though sets are iterable, they are not sequences and do not support indexing and slicing.

Which of the following statements is false? a. Dictionary methods keys and values can be used to iterate through a dictionary's keys or values, respectively. b. Dictionary methods items, keys and values each return a view of a dictionary's data. c. When you iterate over a view, it "sees" a copy of the dictionary's current contents. d. You should not modify a dictionary while iterating through a view. According to the Python Standard Library documentation, either you'll get a RuntimeError or the loop might not process all of the view's values. a

Answer: c. Actually, when you iterate over a view, it "sees" the dictionary's current contents—it does not have its own copy of the dat

Which of the following statements about the following code is false? word_counts = {} for word in text.split(): if word in word_counts: word_counts[word] += 1 else: word_counts[word] = 1 a. The expression text.split() tokenizes text by calling string method split, which separates the words using the method's delimiter string argument—if you do not provide an argument, split uses a space. b. Method split returns a list of tokens (that is, the words in text). c. The expression word_counts[word] += 1 inserts a new key-value pair in the dictionary. d. All of the above statements are true. .

Answer: c. Actually, word_counts[word] += 1 adds 1 to the value of an existing key-value pair in the dictionary

Which of the following statements a), b) or c) is false? a. The following call to dictionary method update receives a dictionary containing one key-value pair to insert or update: country_codes.update({'South Africa': 'za'}) b. Method update can convert keyword arguments into key-value pairs to insert. The following call converts the parameter name Australia into the string key 'Australia' and associates the incorrect value 'ar' with that key: country_codes.update(Australia='ar') c. The snippet in Part (b) provided an incorrect country code ('ar') for Australia. The following code corrects this by using a keyword argument to update the value associated with 'Australia': country_codes.update(Australia='au') d. All of the above statements are true

Answer: d

Which of the following statements a), b) or c) is false? a. The following code builds a dictionary to count the number of occurrences of each word in a string named text—the dictionary's keys will be the unique words, and its values will be integer counts of how many times each word appears in text: word_counts = {} for word in text.split(): if word in word_counts: word_counts[word] += 1 else: word_counts[word] = 1 b. Breaking a string into words is known as tokenizing the string. c. Python automatically concatenates strings separated by whitespace in parentheses. d. All of the above statements are true.

Answer: d.

Which of the following statements a), b) or c) is false? a. The following code creates the dictionary roman_numerals, which maps roman numerals to their integer equivalents (the value for 'X' is intentionally wrong): roman_numerals = {'I' : 1 , 'II' : 2 , 'III' : 3 , 'V' : 5 , 'X' : 100 } b. The following code gets the value associated with the key 'V' in Part (a): roman_numerals['V' ] c. You can update a key's associated value in an assignment statement. The following code replaces the incorrect value associated with the key 'X' in Part (a): roman_numerals['X' ] = 10 d. All of the above statements are true.

Answer: d.

Which of the following statements a), b) or c) is false? a. You can calculate the intersection with the & operator or with the set type's intersection method. b. The difference between two sets is a set consisting of the elements in the left operand that are not in the right operand. c. You can calculate the difference with the - operator or with the a set's difference method. d. All of the above statements are true.

Answer: d.

Which of the following statements about FuncAnimation is false? a. FuncAnimation has two required arguments—the Figure object in which to display the animation, and a function to call once per animation frame. b. The FuncAnimation argument interval is the number of milliseconds between animation frames (the default is 200). c. The FuncAnimation argument fargs (short for "function arguments") is a tuple of other arguments to pass to the function you specify in FuncAnimation's second argument. d. All of the above statements are true.

Answer: d.

Which of the following statements is false? a. When you pass a dictionary to built-in function len, the function returns the number of key-value pairsin the dictionary. b. You can use a dictionary as a condition to determine if it's empty—a non-empty dictionary evaluates to True. c. An empty dictionary evaluates to False. d. Method clean deletes the dictionary's key-value pairs:

Answer: d. Actually, method clear deletes the dictionary's key-value pairs.

Which of the following statements is false? a. The <= operator tests whether the set to its left is an improper subset of the one to its right—that is, all the elements in the left operand are in the right operand, and the sets might be equal. b. You also can check for an improper subset with the set method issubset. c. You can check for an improper superset with the set method issuperset. d. The argument to issubset or issuperset must be a set.

Answer: d. Actually, the argument to issubset or issuperset can be any iterable. When either of these methods receives a non-set iterable argument, it first converts the iterable to a set, then performs the operation.

Which of the following statements is false? a. Set method add inserts its argument if the argument is not already in the set; otherwise, the set remains unchanged. b. Set method remove removes its argument from a set. A KeyError occurs if the value is not in the set. c. Method discard also removes its argument from a set but does not cause an exception if the value is not in the set. d. You can remove the first element of a set with pop.

Answer: d. Actually, you can remove an arbitrary set element and return it with pop—sets are unordered, so you do not know which element will be returned.


Ensembles d'études connexes

International Supply Chain Test 2

View Set

handpicked questions and rationales

View Set

ECON 202 Exam #1 Self-Study Guide

View Set

70-698 all 15 modules 274 total questions

View Set

Chapter 12- Life Insurance Contractual Provisions

View Set

5 Postulates of the Kinetic Molecular Theory (KMT)

View Set