WITH CORRECT ANSWERS
Which of the following would you use if an element is to be removed from a specific
index?
a remove method
an index method
a del statement
a slice method - Answer-a del statement
What will be the value of the variable num_list after the following code executes?
num_list = [1, 2, 3, 4]
num_list[3] = 10
[1, 2, 3, 10]
[1, 2, 10, 4]
[1, 10, 10, 10]
Nothing; this code is invalid - Answer-[1, 2, 3, 10]
The ________ method is commonly used to add items to a list. - Answer-append
Rewrite the following code with list comprehension:
list1 = [4, 7, 12, 8, 25]
list2 = []
for x in list1:
if x > 10:
list2.append(x*2)
A. list1 = [4, 7, 12, 8, 25]list2 = [x*2 for x in list1 if x > 10]
B. list1 = [4, 7, 12, 8, 25]list2 = x*2 for x in list1 if x > 10
C. list1 = [4, 7, 12, 8, 25]list2 = [for x*2 in list1 if x > 10]
D. list1 = [4, 7, 12, 8, 25]list2 = [x*2 if x > 10 in list1 ] - Answer-A. list1 = [4, 7, 12, 8,
25]list2 = [x*2 for x in list1 if x > 10]
The primary difference between a tuple and a list is that
you don't use commas to separate elements in a tuple
, once a tuple is created, it cannot be changed
a tuple cannot include lists as elements
a tuple can only include string elements - Answer-once a tuple is created, it cannot be
changed
What will be the value of the variable list after the following code executes?
list = [1, 2]
list = list * 3
[3, 6]
[1, 2], [1, 2], [1, 2]
[1, 2, 1, 2, 1, 2]
[1, 2] * 3 - Answer-[1, 2, 1, 2, 1, 2]
When working with multiple sets of data, like a table made up of rows and columns, one
would typically use a(n)
tuple
list
nested list
sequence - Answer-nested list
Which of the follow will display "Found it!" if the string 'May 4' is found in the list
appointments? - Answer-if 'May 4' in appointments:
print('Found it!')
The index of the first element in a list is 1, the index of the second element is 2, and so
forth. TRUE OR FALSE - Answer-fALSE
The ________ function can be used to convert a list to a tuple. - Answer-TUPLE
Given the following list comprehension, write the equivalent code without using list
comprehension:
list1 =[[x,y] for x in range(3) for y in range(2) if (x + y) % 2 == 0] - Answer-.
list1 = []
for x in range(3):
for y in range(2):
if (x + y ) % 2 == 0:
list1.append([x,y])
What is an advantage of using a tuple rather than a list?
Tuples can include any data as an element.
Tuples are not limited in size.
Processing a tuple is faster than processing a list.