Deck 5: Sequences: Lists and Tuples  

Full screen (f)
exit full mode
Question
Which of the following statements is false?

A) Collections are prepackaged data structures consisting of related data items.
B) Examples of collections include your favorite songs on your smartphone, your contacts list, a library's books, your cards in a card game, your favorite sports team's players, the stocks in an investment portfolio, patients in a cancer study and a shopping list.
C) Lists are modifiable and tuples are not. Each can hold items of the same or different types.
D) Tuples can dynamically resize as necessary, growing and shrinking at execution time.
Use Space or
up arrow
down arrow
to flip the card.
Question
Lists may store ________ data, that is, data of many different types.

A) parallel
B) heterogeneous
C) homogeneous
D) None of the above.
Question
Which of the following statements is false?

A) The += augmented assignment statement can be used with strings and tuples, even though they're immutable.
B) In the following snippets, after the two assignments, tuple1 and tuple2 are two different copies of the same tuple object: In [1]: tuple1 = (10, 20, 30)
In [2]: tuple2 = tuple1
In [3]: tuple2
Out[3]: (10, 20, 30)
C) Concatenating the tuple (40, 50) to tuple1 from Part (b), as in tuple1 += (40, 50)
Creates a new tuple, then assigns a reference to it to the variable tuple1-tuple2 still refers to the original tuple.
D) For a string or tuple, the item to the right of += must be a string or tuple, respectively-mixing types causes a xe "TypeError"TypeError.
Question
Which of the following statements is false?

A) When you specify a slice and omit the xe "start index of a slice"xe "slice:start index"starting index, 0 is assumed. So, the slice numbers[:6] is equivalent to the slice numbers[0:6]. If you omit the xe "end index of a slice"xe "slice:end index"ending index, Python assumes the sequence's length.
B) Omitting both the start and end indices on a slice copies the entire sequence.
C) Slices makexe "shallow copy" deep copies of the sequence's elements.
D) With slices, the new sequence's elements refer to the same objects as the original sequence's elements, rather than to separate copies.
Question
2 Q5:
A) We've replaced the results of the four list comparisons in snippets [4] through [7] below with ???. What are those four values? In [1]: a = [1, 2, 3]
In [2]: b = [1, 2, 3]
In [3]: c = [1, 2, 3, 4]
In [4]: a == b
Out[4]: ???
In [5]: a == c
Out[5]: ???
In [6]: a < c
Out[6]: ???
In [7]: c >= b
Out[7]: ???

A) False, True, False, False.
B) True, False, False, True.
C) True, False, True, True.
D) True, True, True, False.
Question
Which of the following statements is false?

A) The function modify_elements multiplies each element of its list argument by 2: def modify_elements(items):
""""Multiplies all element values in items by 2."""
For i in range(len(items)):
Items[i] *= 2
B) Part (a)'s function modify_elements' items parameter receives a reference to the original list, so the statement in the loop's suite modifies each element in the original list object.
C) When you pass a tuple to a function, attempting to modify the tuple's immutable elements results in a TypeError.
D) Tuples may contain mutable objects, such as lists, but those objects cannot be modified when a tuple is passed to a function.
Question
Which of the following statements is false?

A) You can use del with a list to remove the element at any valid index or the element(s) from any valid slice.
B) The following code creates a list, then uses del to remove its last element: numbers = list(range(0, 10))
Del numbers[1]
C) The following deletes the list's first two elements: del numbers[0:2]
D) The following uses a step in the slice to delete every other element from the entire list: del numbers[::2]
Question
Which of the following statements is false?

A) Tuples are immutable.
B) Tuples must store xe "heterogeneous data"heterogeneous data (that is, data of different types).
C) A tuple's length is its number of elements.
D) A tuple's length cannot change during program execution.
Question
Consider the list c: c = [-45, 6, 0, 72, 1543]
Which of the following statements a), b) or c) is false?

A) You reference a list element by writing the list's name followed by the element's index (that is, its position number) enclosed in square brackets ([], known as the subscription operator).
B) The names of c's elements are c[0], c[1], c[2], c[3] and c[4].
C) The length of c is 5.
D) All of the above statements are true.
Question
Which of the following statements a), b) or c) is false?

A) You can xe "unpacking a tuple"unpack any sequence's elements by assigning the sequence to a comma-separated list of variables.
B) A xe "ValueError"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.
Question
Which of the following statements is false?

A) Lists are xe "mutable (modifiable):sequence"mutable-their elements can be modified.
B) You can insert and delete list elements, changing the list's length.
C) You can get the individual characters in a string, and you can assign a new value to one of the string's characters.
D) Python's string and tuple sequences are xe "immutable:sequence"immutable-they cannot be modified.
Question
Which of the following statements is false?

A) You can xe "packing a tuple"pack a tuple by separating its values with commas.
B) When you output a tuple, Python always displays its contents in parentheses.
C) You may surround a tuple's comma-separated list of values with optional parentheses.
D) The following statement creates a xe "singleton tuple"xe ", (comma) in singleton tuple"xe "tuple:one-element"one-element tuple: a_singleton_tuple = ('red')
Question
Which of the following statements is false?

A) Usually, you iterate over a tuple's elements.
B) Like list indices, tuple indices start at 0.
C) The following snippets create a time_tuple representing an hour, minute and second, display the tuple, then use its elements to calculate the number of seconds since midnight: In [1]: time_tuple = (9, 16, 1)
In [2]: time_tuple
Out[2]: (9, 16, 1)
In [3]: time_tuple[0] * 3600 + time_tuple[1] * 60 + time_tuple[2]
Out[3]: 33361
D) Assigning a value to a tuple element causes a xe "TypeError"TypeError.
Question
Which of the following statements a), b) or c) is false?

A) You can xe "sequence:concatenate"xe "concatenate sequences"concatenate two lists, two tuples or two strings using the xe "sequence:+ operator for concatenation"xe "+ operator for sequence concatenation"+ operator. The result is a new sequence of the same type containing the left operand's elements followed by the right operand's elements.
B) A TypeError occurs if the + operator's operands are different sequence types-for example, concatenating a list and a tuple is an error.
C) List elements can be accessed via their indices and the subscription operator ([]).
D) All of the above statements are true.
Question
Which of the following statements is false?

A) You can xe "slice"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.
Question
Which of the following statements is false?

A) The following code deletes all of the list's elements: del numbers[:]
B) The del statement can delete any variable.
C) The following code deletes the variable numbers from the interactive session: del numbers
D) After deleting numbers from the interactive session, attempting to display it displays an empty list.
Question
Which of the following statements a), b) or c) is false?

A) The following code creates a student_tuple with a first name, last name and list of grades: student_tuple = ('Amanda', 'Blue', [98, 75, 87])
B) Even though the tuple in Part (a) is immutable, its list element is mutable.
C) In the expression student_tuple[2][1], Python views student_tuple[2] as the element of the tuple containing the list [98, 75, 87], then uses [1] to access the list element containing 75.
D) All of the above statements are true.
Question
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 containing 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.
Question
Which of the following statements a), b) or c) is false?

A) You can use a negative step to select slices in reverse order.
B) The following code creates a new list in reverse order: numbers[::-1]
C) You can modify a list by assigning to a slice of it-the rest of the list is unchanged.
D) All of the above statements are true.
Question
Which of the following statements is false?

A) The following code deletes only the first three elements of numbers by assigning an empty list to the three-element slice: numbers[0:3] = []
B) The following code assigns a list's elements to a slice of every other element of numbers: numbers = [2, 3, 5, 7, 11, 13, 17, 19]
Numbers[2:2:2] = [100, 100, 100, 100]
C) The following code deletes all the elements in numbers, leaving the existing list empty: numbers[:] = []
D) When you assign a new object to a variable the original object that the variable referenced will be garbage collected if no other variables refer to it.
Question
Which of the following statements is false?

A) Often, you'll want to determine whether a sequence (such as a list, tuple or string) contains a value that matches a particular key value.
B) Searching is the process of locating a key in a sequence.
C) List method index takes as an argument a search key-the value to locate in the list-then searches through the list from index 1 and returns the index of the first element that matches the search key.
D) List method index raises a ValueError if the value you're searching for is not in the list.
Question
Which of the following statements a), b) or c) is false?

A) In a call to list method index, specifying the starting and ending indices (as the second and third arguments) causes index to search from the starting index up to but not including the ending index location.
B) The call: numbers.index(5, 7)
Is equivalent to:
Numbers.index(5, 7, len(numbers))
C) The following looks for the value 7 in the range of elements with indices 0 through 4: numbers.index(7, 0, 4)
D) All of the above statements are true.
Question
Which of the following statements is false?

A) The built-in function xe "built-in function:any"xe "any built-in function"any returns True if any item in its iterable argument is True.
B) The built-in function xe "built-in function:all"xe "all built-in function"all returns True if all items in its iterable argument are True.
C) Non-empty iterable objects also evaluate to True, whereas any empty iterable evaluates to False.
D) Functions any and all are examples of external iteration in xe "functional-style programming"functional-style programming.
Question
Which of the following statements a), b) or c) is false?

A) You can use xe "list:*="xe "*= for lists"*= to xe "multiply a sequence"multiply a sequence-that is, append a sequence to itself multiple times.
B) After the following snippet, the list numbers contains two copies of the original list's contents: numbers *= 2
C) The following code searches the list numbers for the value 5 starting from index 7 and continuing through the end of the list: numbers.index(5, 7)
D) All of the above statements are true.
Question
Which of the following statements a), b) or c) is false?

A) You can use list method sort as follows to arrange a list's elements in ascending order: numbers = [10, 3, 7, 1, 9, 4, 2, 8, 5, 6]
Numbers.sort()
B) To sort a list in descending order, call list method sort with the optional keyword argument reverse=False.
C) Built-in function xe "built-in function:sorted"xe "sorted built-in function"sorted returns a new list containing the sorted elements of its argument sequence-the original sequence is unmodified.
D) All of the above statements are true.
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/25
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 5: Sequences: Lists and Tuples  
1
Which of the following statements is false?

A) Collections are prepackaged data structures consisting of related data items.
B) Examples of collections include your favorite songs on your smartphone, your contacts list, a library's books, your cards in a card game, your favorite sports team's players, the stocks in an investment portfolio, patients in a cancer study and a shopping list.
C) Lists are modifiable and tuples are not. Each can hold items of the same or different types.
D) Tuples can dynamically resize as necessary, growing and shrinking at execution time.
D
2
Lists may store ________ data, that is, data of many different types.

A) parallel
B) heterogeneous
C) homogeneous
D) None of the above.
B
3
Which of the following statements is false?

A) The += augmented assignment statement can be used with strings and tuples, even though they're immutable.
B) In the following snippets, after the two assignments, tuple1 and tuple2 are two different copies of the same tuple object: In [1]: tuple1 = (10, 20, 30)
In [2]: tuple2 = tuple1
In [3]: tuple2
Out[3]: (10, 20, 30)
C) Concatenating the tuple (40, 50) to tuple1 from Part (b), as in tuple1 += (40, 50)
Creates a new tuple, then assigns a reference to it to the variable tuple1-tuple2 still refers to the original tuple.
D) For a string or tuple, the item to the right of += must be a string or tuple, respectively-mixing types causes a xe "TypeError"TypeError.
B
4
Which of the following statements is false?

A) When you specify a slice and omit the xe "start index of a slice"xe "slice:start index"starting index, 0 is assumed. So, the slice numbers[:6] is equivalent to the slice numbers[0:6]. If you omit the xe "end index of a slice"xe "slice:end index"ending index, Python assumes the sequence's length.
B) Omitting both the start and end indices on a slice copies the entire sequence.
C) Slices makexe "shallow copy" deep copies of the sequence's elements.
D) With slices, the new sequence's elements refer to the same objects as the original sequence's elements, rather than to separate copies.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
5
2 Q5:
A) We've replaced the results of the four list comparisons in snippets [4] through [7] below with ???. What are those four values? In [1]: a = [1, 2, 3]
In [2]: b = [1, 2, 3]
In [3]: c = [1, 2, 3, 4]
In [4]: a == b
Out[4]: ???
In [5]: a == c
Out[5]: ???
In [6]: a < c
Out[6]: ???
In [7]: c >= b
Out[7]: ???

A) False, True, False, False.
B) True, False, False, True.
C) True, False, True, True.
D) True, True, True, False.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
6
Which of the following statements is false?

A) The function modify_elements multiplies each element of its list argument by 2: def modify_elements(items):
""""Multiplies all element values in items by 2."""
For i in range(len(items)):
Items[i] *= 2
B) Part (a)'s function modify_elements' items parameter receives a reference to the original list, so the statement in the loop's suite modifies each element in the original list object.
C) When you pass a tuple to a function, attempting to modify the tuple's immutable elements results in a TypeError.
D) Tuples may contain mutable objects, such as lists, but those objects cannot be modified when a tuple is passed to a function.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
7
Which of the following statements is false?

A) You can use del with a list to remove the element at any valid index or the element(s) from any valid slice.
B) The following code creates a list, then uses del to remove its last element: numbers = list(range(0, 10))
Del numbers[1]
C) The following deletes the list's first two elements: del numbers[0:2]
D) The following uses a step in the slice to delete every other element from the entire list: del numbers[::2]
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
8
Which of the following statements is false?

A) Tuples are immutable.
B) Tuples must store xe "heterogeneous data"heterogeneous data (that is, data of different types).
C) A tuple's length is its number of elements.
D) A tuple's length cannot change during program execution.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
9
Consider the list c: c = [-45, 6, 0, 72, 1543]
Which of the following statements a), b) or c) is false?

A) You reference a list element by writing the list's name followed by the element's index (that is, its position number) enclosed in square brackets ([], known as the subscription operator).
B) The names of c's elements are c[0], c[1], c[2], c[3] and c[4].
C) The length of c is 5.
D) All of the above statements are true.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
10
Which of the following statements a), b) or c) is false?

A) You can xe "unpacking a tuple"unpack any sequence's elements by assigning the sequence to a comma-separated list of variables.
B) A xe "ValueError"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.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
11
Which of the following statements is false?

A) Lists are xe "mutable (modifiable):sequence"mutable-their elements can be modified.
B) You can insert and delete list elements, changing the list's length.
C) You can get the individual characters in a string, and you can assign a new value to one of the string's characters.
D) Python's string and tuple sequences are xe "immutable:sequence"immutable-they cannot be modified.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
12
Which of the following statements is false?

A) You can xe "packing a tuple"pack a tuple by separating its values with commas.
B) When you output a tuple, Python always displays its contents in parentheses.
C) You may surround a tuple's comma-separated list of values with optional parentheses.
D) The following statement creates a xe "singleton tuple"xe ", (comma) in singleton tuple"xe "tuple:one-element"one-element tuple: a_singleton_tuple = ('red')
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
13
Which of the following statements is false?

A) Usually, you iterate over a tuple's elements.
B) Like list indices, tuple indices start at 0.
C) The following snippets create a time_tuple representing an hour, minute and second, display the tuple, then use its elements to calculate the number of seconds since midnight: In [1]: time_tuple = (9, 16, 1)
In [2]: time_tuple
Out[2]: (9, 16, 1)
In [3]: time_tuple[0] * 3600 + time_tuple[1] * 60 + time_tuple[2]
Out[3]: 33361
D) Assigning a value to a tuple element causes a xe "TypeError"TypeError.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
14
Which of the following statements a), b) or c) is false?

A) You can xe "sequence:concatenate"xe "concatenate sequences"concatenate two lists, two tuples or two strings using the xe "sequence:+ operator for concatenation"xe "+ operator for sequence concatenation"+ operator. The result is a new sequence of the same type containing the left operand's elements followed by the right operand's elements.
B) A TypeError occurs if the + operator's operands are different sequence types-for example, concatenating a list and a tuple is an error.
C) List elements can be accessed via their indices and the subscription operator ([]).
D) All of the above statements are true.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
15
Which of the following statements is false?

A) You can xe "slice"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.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
16
Which of the following statements is false?

A) The following code deletes all of the list's elements: del numbers[:]
B) The del statement can delete any variable.
C) The following code deletes the variable numbers from the interactive session: del numbers
D) After deleting numbers from the interactive session, attempting to display it displays an empty list.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
17
Which of the following statements a), b) or c) is false?

A) The following code creates a student_tuple with a first name, last name and list of grades: student_tuple = ('Amanda', 'Blue', [98, 75, 87])
B) Even though the tuple in Part (a) is immutable, its list element is mutable.
C) In the expression student_tuple[2][1], Python views student_tuple[2] as the element of the tuple containing the list [98, 75, 87], then uses [1] to access the list element containing 75.
D) All of the above statements are true.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
18
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 containing 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.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
19
Which of the following statements a), b) or c) is false?

A) You can use a negative step to select slices in reverse order.
B) The following code creates a new list in reverse order: numbers[::-1]
C) You can modify a list by assigning to a slice of it-the rest of the list is unchanged.
D) All of the above statements are true.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
20
Which of the following statements is false?

A) The following code deletes only the first three elements of numbers by assigning an empty list to the three-element slice: numbers[0:3] = []
B) The following code assigns a list's elements to a slice of every other element of numbers: numbers = [2, 3, 5, 7, 11, 13, 17, 19]
Numbers[2:2:2] = [100, 100, 100, 100]
C) The following code deletes all the elements in numbers, leaving the existing list empty: numbers[:] = []
D) When you assign a new object to a variable the original object that the variable referenced will be garbage collected if no other variables refer to it.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
21
Which of the following statements is false?

A) Often, you'll want to determine whether a sequence (such as a list, tuple or string) contains a value that matches a particular key value.
B) Searching is the process of locating a key in a sequence.
C) List method index takes as an argument a search key-the value to locate in the list-then searches through the list from index 1 and returns the index of the first element that matches the search key.
D) List method index raises a ValueError if the value you're searching for is not in the list.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
22
Which of the following statements a), b) or c) is false?

A) In a call to list method index, specifying the starting and ending indices (as the second and third arguments) causes index to search from the starting index up to but not including the ending index location.
B) The call: numbers.index(5, 7)
Is equivalent to:
Numbers.index(5, 7, len(numbers))
C) The following looks for the value 7 in the range of elements with indices 0 through 4: numbers.index(7, 0, 4)
D) All of the above statements are true.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
23
Which of the following statements is false?

A) The built-in function xe "built-in function:any"xe "any built-in function"any returns True if any item in its iterable argument is True.
B) The built-in function xe "built-in function:all"xe "all built-in function"all returns True if all items in its iterable argument are True.
C) Non-empty iterable objects also evaluate to True, whereas any empty iterable evaluates to False.
D) Functions any and all are examples of external iteration in xe "functional-style programming"functional-style programming.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
24
Which of the following statements a), b) or c) is false?

A) You can use xe "list:*="xe "*= for lists"*= to xe "multiply a sequence"multiply a sequence-that is, append a sequence to itself multiple times.
B) After the following snippet, the list numbers contains two copies of the original list's contents: numbers *= 2
C) The following code searches the list numbers for the value 5 starting from index 7 and continuing through the end of the list: numbers.index(5, 7)
D) All of the above statements are true.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
25
Which of the following statements a), b) or c) is false?

A) You can use list method sort as follows to arrange a list's elements in ascending order: numbers = [10, 3, 7, 1, 9, 4, 2, 8, 5, 6]
Numbers.sort()
B) To sort a list in descending order, call list method sort with the optional keyword argument reverse=False.
C) Built-in function xe "built-in function:sorted"xe "sorted built-in function"sorted returns a new list containing the sorted elements of its argument sequence-the original sequence is unmodified.
D) All of the above statements are true.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 25 flashcards in this deck.