[alist] * 2 creates a list containing alist repeated 2 times
[[4, 2, 8, 999, 5], [4, 2, 8, 999, 5]]
Yes, blist contains two references, both to alist.
[4, 2, 8, 6, 5]
print(blist)
[[4, 2, 8, 999, 5], [4, 2, 8, 6, 5]]
blist contains two references, both to alist so changes to alist appear both times.
6.
Q-6: What is returned by the following function?
def list_transformation():
alist = [4, 2, 8, 6, 5]
blist = [ ]
for item in alist:
blist.append(item+5)
return blist
[4, 2, 8, 6, 5]
5 is added to each item before the append is performed.
[4, 2, 8, 6, 5, 5]
There are too many items in this list. Only 5 append operations are performed.
[9, 7, 13, 11, 10]
Yes, the for loop processes each item of the list. 5 is added before it is appended to blist.
Error, you cannot concatenate inside an append.
5 is added to each item before the append is performed.
7.
Q-7: Which method would you use to figure out the position of an item in a list?
.pop()
pop removes and returns items (default is to remove and return the last item in the list)
.insert()
insert will add an item at whatever position is specified.
.count()
count returns the number of times something occurs in a list
.index()
Yes, index will return the position of the first occurrence of an item.
8.
Q-8: Which method is best to use when adding an item to the end of a list?
.insert()
While you can use insert, it is not the best method to use because you need to specify that you want to stick the new item at the end.
.pop()
pop removes an item from a list
.append()
Yes, though you can use insert to do the same thing, you don't need to provide the position.
.remove()
remove gets rid of the first occurrence of any item that it is told. It does not add an item.
9.
Q-9: Given that we want to accumulate the total sum of a list of numbers, which of the following accumulator patterns would be appropriate?
def find_sum():
nums = [4, 5, 2, 93, 3, 5]
s = 0
for n in nums:
s = s + 1
return s
def find_sum():
nums = [4, 5, 2, 93, 3, 5]
s = 0
for n in nums:
s = n + n
return s
def find_sum():
nums = [4, 5, 2, 93, 3, 5]
s = 0
for n in nums:
s = s + n
return s
Pattern I
This pattern will only count how many items are in the list, not provide the total accumulated value.
Pattern II
This would reset the value of s each time the for loop iterated, and so by the end s would be assigned the value of the last item in the list plus the last item in the list.
Pattern III
Yes, this will solve the problem.
none of the above would be appropriate for the problem.
One of the patterns above is a correct way to solve the problem.
10.
Q-10: Given that we want to accumulate the total number of strings in the list, which of the following accumulator patterns would be appropriate?
def num_of_strings():
lst = ['plan', 'answer', 5, 9.29, 'order, items', [4]]
s = 0
for n in lst:
s = s + n
return s
def num_of_strings():
lst = ['plan', 'answer', 5, 9.29, 'order, items', [4]]
for item in lst:
s = 0
if isinstance(item, str):
s = s + 1
return s
def num_of_strings():
lst = ['plan', 'answer', 5, 9.29, 'order, items', [4]]
s = ""
for n in lst:
s = s + n
return s
def num_of_strings():
lst = ['plan', 'answer', 5, 9.29, 'order, items', [4]]
s = 0
for item in lst:
if isinstance(item, str):
s = s + 1
Pattern 1.
How does this solution know that the element of lst is a string and that s should be updated?
Pattern 2.
What happens to s each time the for loop iterates?
Pattern 3.
Reread the prompt again. What do we want to accumulate?
Pattern 4.
Yes, this will solve the problem.
none of the above would be appropriate for the problem.
One of the patterns above is a correct way to solve the problem.
11.
Q-11: What will the following code print?
def mystery(num_list):
index = 0
while index < len(num_list):
num = num_list[index]
if num == 0:
num_list.pop(index)
index += 1
list1 = [3, 0, 2, 0, 0]
mystery(list1)
print(list1)
[3, 0, 2, 0, 0]
The list is modified by the pop.
[3, 0, 2]
It will pop 0's except for the last one.
[3, 2]
This would be true if it didn't always increment the index.
[3, 2, 0]
Since it always increments the index it will miss the last zero.
12.
Q-12: What will the following code print?
def mystery(num_list):
sum = 0
for i in range(0, len(num_list), 2):
num = num_list[i]
sum += num
return sum
list1 = [1, 2, 3, 4, 5]
print(mystery(list1))
1
It adds 1 to the sum and then returns.
9
This would be true if it didn't return until after the loop finished
15
This would be true if it didn't return until after the loop finished and the range incremented by 1 rather than 2
None
This would be true if there wasn't a return statement
13.
Q-13: What will the following code print?
def mystery(num_list):
for num in num_list:
if num < 0:
return False
else:
return True
print(mystery([3, -1, 2]))
True
It returns true after checking the first num.
False
This would be true if the first number in the list was negative.
None
This would be true if there wasn't a return statement.
It will not compile
This would be true if there was a syntax error.
14.
Q-14: What will the following code print?
def mystery(num_list):
out = []
for num in num_list:
if num > 10:
out.append(num)
return out
print(mystery([5, 10, 15, 20]))
[10, 15, 20]
It only adds numbers that are greater than 10
[20, 15]
This would be true if append added at the front, but it adds at the end
[15, 20]
It adds all numbers greater than 10 in order.
[20, 15, 10]
This would be true if append added at the front, but it adds at the end and it won't add the 10
15.
Q-15: What will the following code print?
def mystery(num_list):
out = []
for i in range(len(num_list) - 1,0,-1):
num = num_list[i]
out.append(num)
return out
print(mystery([5, 10, 15, 20]))
[5, 10, 15, 20]
It adds the numbers in reverse order
[20, 15, 10, 5]
This would be true if the end for range was less than 0
[5, 10, 15]
It adds the numbers in reverse order
[20, 15, 10]
It adds the numbers in reverse order, but stops before the item at index 0.