Exercise: Python

Questions for: Lists

What is the purpose of the index() method in Python lists?
A:
Finds the index of the first occurrence of a specific element.
B:
Finds the index of the last occurrence of a specific element.
C:
Returns the index of the maximum element in the list.
D:
Searches for an element in the list based on a condition.
Answer: A
The index() method in Python lists is used to find the index of the first occurrence of a specific element.
How can you create a new list containing only the unique elements from an existing list in Python?
A:
new_list = list.unique()
B:
new_list = list.distinct()
C:
new_list = list(set(list))
D:
new_list = list.remove_duplicates()
Answer: C
Using set() to remove duplicates and then converting it back to a list creates a new list with unique elements.
How can you find the average of elements in a numeric list in Python?
A:
average(list)
B:
sum(list) / len(list)
C:
mean(list)
D:
list.average()
Answer: B
The average of elements in a numeric list is calculated as the sum divided by the number of elements.
What is the purpose of the sort() method in Python lists?
A:
Reverses the order of elements.
B:
Sorts the list in ascending order.
C:
Removes duplicate elements.
D:
Appends elements to the end of the list.
Answer: B
The sort() method in Python lists is used to sort the list in ascending order.
What does the clear() method do in Python lists?
A:
Removes the last element from the list.
B:
Removes all occurrences of a specific element.
C:
Removes all elements from the list.
D:
Removes elements from the list based on a condition.
Answer: C
The clear() method in Python lists is used to remove all elements from the list, leaving it empty.
Ad Slot (Above Pagination)
Quiz