Back to notes
How do you reverse the order of elements in a list?
Press to flip
Using the reverse() method, e.g., numbers.reverse().
Explain the use of the insert() method in lists.
The insert() method adds an element at a specified index, e.g., numbers.insert(2, 45).
Write a Python snippet to access the last element of a list using negative indexing.
roll_numbers[-1]
Describe how to use slicing to get the first three elements of a list.
numbers[:3]
Write the code to find the index of the first occurrence of the value 7 in a list.
numbers.index(7)
What Python function returns the length of a list?
The len() function, e.g., len(numbers).
How would you sort a list in ascending order?
Using the sort() method, e.g., numbers.sort().
Explain the function of the pop() method when no index is provided.
The pop() method removes and returns the last element of the list.
Write a Python snippet to add multiple elements (45, 46, 47) to the end of a list.
numbers.extend([45, 46, 47])
How can you remove the first occurrence of a specific value (45) from a list?
Using the remove() method, e.g., numbers.remove(45).
What are the key characteristics of a Python list?
Python lists are ordered, mutable, and allow duplicates.
Demonstrate with code how to slice a list from the second element to the end.
numbers[1:]
How would you create a list with heterogeneous data types?
Example: mixed_list = [1, 'Hello', True, 10.5]
What does the count() method do in the context of lists?
The count() method returns the number of occurrences of a specified value in the list.
What is the difference between the append() and extend() methods in lists?
append() adds a single element to the end of the list, while extend() adds multiple elements.
Previous
Next