10 Review
10 Review
1. emptyList = []
lst = [1, 32, 2]
2.
There are 6 elements in the list. The index of the first element is 0. The index of the last
element is 5. lst[2] is 12. list[-2] is list[6-2], which is 10.
3.
lst.append(40) => [30, 1, 2, 1, 0, 40]
lst.insert(1, 43) => [30, 43, 1, 2, 1, 0]
lst.extend([1, 43]) => [30, 1, 2, 1, 0, 1, 43]
lst.remove(1) => [30, 2, 1, 0]
lst.pop(1) => [30, 2, 1, 0]
lst.pop() => [30, 1, 2, 1]
lst.sort() => [0, 1, 1, 2, 30]
lst.reverse() => [0, 1, 2, 1, 30]
random.shuffle(list) => list is randomly shuffled
4.
lst.index(1) => 1
lst.count(1) => 2
len(list) => 5
max(list) => 30
min(list) => 0
sum(list) => 34
5.
list1 + list2 => [30, 1, 2, 1, 0, 1, 21, 13]
2 * list2 => [1, 21, 13, 1, 21, 13]
list2 * 2 => [1, 21, 13, 1, 21, 13]
list1[1 : 3] => [1, 2]
list1[3] => 1
6.
7.
list1 < list2 => False
list1 <= list2 => False
list1 == list2 => False
list1 != list2 => True
list1 > list2 => True
list1 >= list2 => True
8.
Indicate true or false for the following statements:
Every element in a list must have the same type. (False)
The list size is fixed after it is created. (False)
The list can have duplicated elements. (True)
The elements in a list can be accessed via an indexer.
(True)
9.
10.
list1 is [22, 43]
list2 is [1, 43]
11
list(s)
12.
13.
14.
[1, 1, 1, 1, 1, 1]
15.
[111, 3, 5, 7, 9]
[111, 3, 5, 7, 9]
16.
[111, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
17.
No. The reference of the list is passed to the parameter in the function. No new list is
created.
18.
(a)
number is 0 and numbers[0] is 3
(b)
1 2 3 4 5
This is a tricky question. You need to read the code carefully to get
the correct output. Note that the reverse function is supposed to
reverse the list, but it was coded incorrectly so it did not reverse the
original list. In order to reverse the elements in the original list,
modify the function as follows:
def reverse(lst):
for i in range(int(len(lst) / 2)):
temp = lst[i]
lst[i] = lst[len(lst) - 1 - i]
lst[len(lst) - 1 - i] = temp
19.
(a)
[1, 2, 3]
[2, 3]
(b)
[1, 2, 3]
[1, 2, 3]
20. Omitted
22 Omitted
23 Omitted