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