Sequences: Lists and Tuples Flashcards
(Fill-In) Python’s string and tuple sequences are _______—they cannot be modified.
immutable.
(True/False) The + operator’s sequence operands may be of any sequence type.
False. The + operator’s operand sequences must have the same type; otherwise, a TypeError occurs.
(True/False) A += augmented assignment statement may not be used with strings and tuples, because they’re immutable.
False. A += augmented assignment statement also may be used with strings and tuples, even though they’re immutable. The result is a new string or tuple, respectively.
(True/False) Tuples can contain only immutable objects.
False. Even though a tuple is immutable, its elements can be mutable objects, such as lists.
(Fill-In) A sequence’s elements can be ________ by assigning the sequence to a comma-separated list of variables.
unpacked
(True/False) The following expression causes an error:
’-‘ * 10
False: In this context, the multiplication operator (*) repeats the string (‘-‘) 10 times.
(True/False) Slice operations that modify a sequence work identically for lists, tuples and strings
False. Slice operations that do not modify a sequence work identically for lists, tuples and strings
(Fill-In) Assume you have a list called names. The slice expression ______ creates a new list with the elements of names in reverse order.
names[::-1]
(True/False) You cannot modify a list’s contents when you pass it to a function.
False. When you pass a list (a mutable object) to a function, the function receives a reference to the original list object and can use that reference to modify the original list’s contents
(True/False) Tuples can contain lists and other mutable objects. Those mutable objects can be modified when a tuple is passed to a function.
True
(Fill-In) To sort a list in descending order, call list method sort with the optional keyword argument ______ set to True.
reverse.
(True/False) All sequences provide a sort method.
False. Immutable sequences like tuples and strings do not provide a sort method. However, you can sort any sequence without modifying it by using built-in function sorted, which returns a new list containing the sorted elements of its argument sequence.
(Fill-In) The _______ operator can be used to extend a list with copies of itself.
*=
(Fill-In) Operators ______ and ______ determine whether a sequence contains or does not contain a value, respectively.
in, not in
(Fill-In) To add all the elements of a sequence to the end of a list, use list method _______, which is equivalent to using +=.
extend
(Fill-In) For a list numbers, calling method ________ is equivalent to numbers[:] = [].
clear.
(Fill-In) You can simulate a stack with a list, using methods ______ and _____ to add and remove elements, respectively, only at the end of the list.
append, pop
Fill-In) To prevent an IndexError when calling pop on a list, first ensure that _______.
the list’s length is greater than 0.
(Fill-In) A list comprehension’s ______ clause iterates over the specified sequence.
for.
(Fill-In) A list comprehension’s ______ clause filters sequence elements to select only those that match a condition.
if.
(Fill-In) A generator expression is ______—it produces values on demand
lazy.
(Fill-In) _______, _______ and _______ are common operations used in functional-style programming
Filter, map, reduce.
(Fill-In) A(n) _______ processes a sequence’s elements into a single value, such as their count, total or average.
reduction
(True/False) The letter ‘V’ “comes after” the letter ‘g’ in the alphabet, so the comparison ‘Violet’ < ‘green’ yields False
False. Strings are compared by their characters’ underlying numerical values. Lowercase letters have higher numerical values than uppercase. So, the comparison is True.
(Fill-In) Built-in function ______ returns an iterator that enables you to iterate over a sequence’s values backward.
reversed.
(Fill-In) In a two-dimensional list, the first index by convention identifies the ______ of an element and the second index identifies ______ the of an element.
row, column
(Fill-In) The ________ format specifier indicates that a number should be displayed with thousands separators.
comma (,)
(Fill-In) A Matplotlib ______ object manages the content that appears in a Matplotlib window
Axes
(Fill-In) The Seaborn function ____ displays data as a bar chart.
barplot
(Fill-In) The Matplotlib function ______ displays a plot window from a script.
show.