Chapter Review Questions#
Note
Source: Adapted from the C# edition (lists/reviewlists.rst and
arrays/reviewarrays.rst). Questions updated for Python list syntax
and methods.
What is the index of the first element of a list? The last?
How do you get the number of elements in a list named
data?Two ways to sort.
What does
list.sort()do?What does
sorted(lst)do?When would you prefer each?
What does
data[2:5]return ifdata = [10, 20, 30, 40, 50, 60]?What does
data[-1]refer to?What is the difference between
list.append(x)andlist.extend(other)?What error is raised if you call
data.remove(x)andxis not in the list?The
pop()method.What does
data.pop()return ifdata = [1, 2, 3]?What does
datacontain after the call?
Write a list comprehension that produces the squares of odd numbers from 1 to 9:
[1, 9, 25, 49, 81].Aliasing.
a = [1, 2, 3] b = a b.append(4) print(a)
What is printed?
Why is the output surprising?
How would you make a true independent copy of
a?
Linear search vs. binary search.
What is the time complexity of each?
What additional requirement does binary search impose on the list?
Trace through one complete pass of bubble sort on
[5, 3, 1, 4]and show the state of the list after each swap.