Starting Out with Python 4e (Gaddis)
Chapter 7 Lists and Tuples
TRUE/FALSE
1. Invalid indexes do not cause slicing expressions to raise an exception.
ANS: T
2. Lists are dynamic data structures such that items may be added to them or removed from them.
ANS: T
3. Arrays, which are allowed by most other programming languages, have more capabilities than Python list structures.
ANS: F
4. A list cannot be passed as an argument to a function.
ANS: F
5. The remove method removes all occurrences of an item from a list.
ANS: F
6. The sort method rearranges the elements of a list so they are in ascending or descending order.
ANS: F
7. The index of the first element in a list is 1, the index of the second element is 2, and so forth.
ANS: F
8. The index -1 identifies the last element in a list.
ANS: T
9. To calculate the average of the numeric values in a list, the first step is to get the total of values in the list.
ANS: T
10. In slicing, if the end index specifies a position beyond the end of the list, Python will use the length of the list instead.
ANS: T
11. In order to create graphs using the matplotlib package, you need to import the pyplot module.
ANS: T
12. To add a descriptive label to the X and Y axes of a graph when using the matplotlib package, you need to import the labels module.
ANS: F
MULTIPLE CHOICE
1. What are the data items in a list called?
a.
data
b.
elements
c.
items
d.
values
ANS: B
2. When working with multiple sets of data, one would typically use a(n)
a.
list
b.
tuple
c.
nested list
d.
sequence
ANS: C
3. 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
ANS: D
4. 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.
ANS: C
5. 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]
ANS: D
6. 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
ANS: A
7. What is the first negative index in a list?
a.
0
b.
-1
c.
-0
d.
the size of the list minus 1
ANS: B
8. Which method can be used to place an item at a specific index in a list?
a.
append
b.
index
c.
insert
d.
add
ANS: C
9. Which method or operator can be used to concatenate lists?
a.
*
b.
+
c.
%
d.
concat
ANS: B
10. Which method can be used to convert a list to a tuple?
a.
append
b.
tuple
c.
insert
d.
list
ANS: B
11. Which method can be used to convert a tuple to a list?
a.
append
b.
tuple
c.
insert
d.
list
ANS: D
12. 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]
ANS: C
13. 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
ANS: A
14. 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
ANS: A
15. In order to create a graph in Python, you need to include
a.
import matplotlib
b.
import pyplot
c.
import matplotlib.pyplot
d.
import matplotlib
import pyplot
ANS: C
16. What will be the output after the following code is executed?
import matplotlib.pyplot as plt
def main():
x_crd = [0, 1 , 2, 3, 4, 5]
y_crd = [2, 4, 5, 2]
plt.plot(x_crd, y_crd)
main()
a.
It will display a simple line graph.
b.
It will display a simple bar graph.
c.
Nothing; plt is not a Python method.
d.
Nothing; the number of x-coordinates do not match the number of y-coordinates.
ANS: D
COMPLETION
1. A(n) __________ is an object that holds multiple items of data.
ANS: sequence
2. Each element in a tuple has a(n) __________ that specifies its position in the tuple.
ANS: index
3. The built-in function __________ returns the length of a sequence.
ANS: len
4. Tuples are __________ sequences which means that once a tuple is created, it cannot be changed.
ANS: immutable
5. A(n) ___________ is a span of items that are taken from a sequence.
ANS: slice
6. Lists are ___________, which means their elements can be changed in a program.
ANS: mutable
7. The __________ method is commonly used to add items to a list.
ANS: append
8. The __________ exception is raised when a search item is not in the list being searched.
ANS: ValueError
9. The __________ method reverses the order of the items in a list.
ANS: reverse
10. The __________ function returns the item that has the lowest value in the sequence.
ANS: min
11. The __________ package is a library you can use in Python to create two-dimensional charts and graphs.
ANS: matplotlib
12. The ___________ function can be used to convert a list to a tuple.
ANS: tuple