Chapter 5, Sequences: Lists and Tuples 5
a. You can unpack any sequence’s elements by assigning the sequence to a
comma-separated list of variables.
b. A ValueError occurs if the number of variables to the left of the assignment
symbol is not identical to the number of elements in the sequence on the right.
c. The following code unpacks a sequence produced by range:
number1, number2, number3 = range(3)
d. All of the above statements are true.
5.4 Q2: Which of the following statements a), b) or c) is false?
a. The preferred mechanism for accessing an element’s index and value is the
built-in function enumerate, which receives an iterable and creates an iterator
that, for each element, returns a tuple containing the element’s index and value.
b. The following code uses the built-in function list to create a list of tuples con-
taining enumerate’s results:
colors = [‘red’, ‘orange’, ‘yellow’]
colors_list = list(enumerate(colors))
c. The following for loop unpacks each tuple returned by enumerate into the
variables index and value and displays them:
for index, value in enumerate(colors):
print(f‘{index}: {value}‘)
d. All of the above statements are true.
5.5 Sequence Slicing
5.5 Q1: Which of the following statements is false?
a. You can slice sequences to create new sequences of the same type containing
subsets of the original elements.
b. Slice operations can modify mutable sequences. Slice operations that do not
modify a sequence work identically for lists, tuples and strings.
c. The following code creates a slice consisting of the elements at indices 2
through 6 of the list numbers:
numbers = [2, 3, 5, 7, 11, 13, 17, 19]
numbers2 = numbers[2:6]
d. When taking a slice of a list, the original list is not modified.