Week 7 Flashcards
Invalid indexes do not cause slicing expressions to raise an exception.
True
Lists are dynamic data structures such that items may be added to them or removed from them.
True
Arrays, which are allowed by most other programming languages, have more capabilities than Python list structures.
False
A list cannot be passed as an argument to a function.
False
The remove method removes all occurrences of an item from a list.
False
The sort method rearranges the elements of a list so they are in ascending or descending order.
False
The index of the first element in a list is 1, the index of the second element is 2, and so forth.
False
The index -1 identifies the last element in a list.
True
To calculate the average of the numeric values in a list, the first step is to get the total of values in the list.
True
In slicing, if the end index specifies a position beyond the end of the list, Python will use the length of the list instead.
True
What are the data items in a list called?
a. data
b. elements
c. items
d. values
b. elements
When working with multiple sets of data, one would typically use a(n)
a. list
b. tuple
c. nested list
d. sequence
c. nested list
The primary difference between a tuple and a list is that
a. you don’t use commas to separate elements in a tuple
b. a tuple can only include string elements
c. a tuple cannot include lists as elements
d. once a tuple is created, it cannot be changed
d. once a tuple is created, it cannot be changed
What is an advantage of using a tuple rather than a list?
a. Tuples are not limited in size.
b. Tuples can include any data as an element.
c. Processing a tuple is faster than processing a list.
d. There is never an advantage to using a tuple.
c. Processing a tuple is faster than processing a list.
Which list will be referenced by the variable number after the following code is executed?
number = range(0, 9, 2)
a. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b. [1, 3, 5, 7, 9]
c. [2, 4, 6, 8]
d. [0, 2, 4, 6, 8]
d. [0, 2, 4, 6, 8]
Which of the following would you use if an element is to be removed from a specific index?
a. a del statement
b. a remove method
c. an index method
d. a slice method
a. del statement
What is the first negative index in a list?
a. 0
b. -1
c. -0
d. the size of the list minus 1
b. -1
Which method can be used to place an item at a specific index in a list?
a. append
b. index
c. insert
d. add
c. insert
Which method or operator can be used to concatenate lists?
a. *
b. +
c. %
d. concat
b. +
Which method can be used to convert a list to a tuple?
a. append
b. tuple
c. insert
d. list
b. tuple
Which method can be used to convert a tuple to a list?
a. append
b. tuple
c. insert
d. list
d. list
What will be the value of the variable list after the following code executes?
list = [1, 2]
list = list * 3
a. [1, 2] * 3
b. [3, 6]
c. [1, 2, 1, 2, 1, 2]
d. [1, 2], [1, 2], [1, 2]
c. [1, 2, 1, 2, 1, 2]
What will be the value of the variable list after the following code executes?
list = [1, 2, 3, 4]
list[3] = 10
a. [1, 2, 3, 10]
b. [1, 2, 10, 4]
c. [1, 10, 10, 10]
d. Nothing; this code is invalid
a. [1, 2, 3, 10]
What will be the value of the variable list2 after the following code executes?
list1 = [1, 2, 3]
list2 = []
for element in list1:
list2.append(element)
list1 = [4, 5, 6]
a. [1, 2, 3]
b. [4, 5, 6]
c. [1, 2, 3, 4, 5, 6]
d. Nothing; this code is invalid
a. [1, 2, 3]
What will be the value of the variable list2 after the following code executes?
list1 = [1, 2, 3]
list2 = []
list1 = [4, 5, 6]
a. [1, 2, 3]
b. [4, 5, 6]
c. [1, 2, 3, 4, 5, 6]
d. Nothing; this code is invalid
b. [4, 5, 6]
A(n) __________ is an object that holds multiple items of data.
sequence
Each element in a tuple has a(n) __________ that specifies its position in the tuple.
index
The built-in function __________ returns the length of a sequence.
len
Tuples are __________ sequences which means that once a tuple is created, it cannot be changed.
immutable
A(n) ___________ is a span of items that are taken from a sequence.
slice
Lists are ___________, which means their elements can be changed in a program.
mutable
The __________ method is commonly used to add items to a list.
append
The __________ exception is raised when a search item is not in the list being searched.
ValueError
The __________ method reverses the order of the items in a list.
reverse
The __________ function returns the item that has the lowest value in the sequence.
min
The ___________ function can be used to convert a list to a tuple.