Slicing in Python refers to the technique of extracting a portion of a list (or any iterable) by specifying a range of indices. It allows you to create a new list containing elements from the original list within the specified range. Slicing is done using the colon :
operator within square brackets []
.
Here’s how slicing works:
- The basic syntax for slicing is
my_list[start:end]
, where:start
is the index of the first element to include in the slice (inclusive).end
is the index of the first element to exclude from the slice (exclusive).
- If
start
is omitted, it defaults to 0 (the beginning of the list). - If
end
is omitted, it defaults to the length of the list (the end of the list).
Slicing returns a new list containing the selected elements, leaving the original list unchanged.
Examples:
python
my_list = [1, 2, 3, 4, 5] # Extract elements from index 1 to 3 (exclusive) result = my_list[1:3] # Result: [2, 3] # Extract elements from the beginning up to index 3 (exclusive) result = my_list[:3] # Result: [1, 2, 3] # Extract elements from index 2 to the end result = my_list[2:] # Result: [3, 4, 5]
Now, let’s create some MCQs based on slicing in Python lists:
MCQ 1: What is slicing in Python?
- A) Dividing a list into multiple sublists
- B) Extracting a portion of a list based on indices
- C) Sorting a list in ascending order
- D) Combining two lists into one
Answer: B) Extracting a portion of a list based on indices
- Explanation: Slicing in Python involves extracting a subset of elements from a list using index ranges.
MCQ 2: In Python slicing, what does the end
index represent in my_list[start:end]
?
- A) The last element included in the slice
- B) The first element excluded from the slice
- C) The total number of elements in the list
- D) The index of the last element in the list
Answer: B) The first element excluded from the slice
- Explanation: In Python slicing, the
end
index represents the first element that is excluded from the slice.
MCQ 3: What is the default value for the start
index in Python slicing if it is omitted?
- A) 0 (the beginning of the list)
- B) 1 (the first element of the list)
- C) -1 (the last element of the list)
- D) None (no default value)
Answer: A) 0 (the beginning of the list)
- Explanation: If the
start
index is omitted in Python slicing, it defaults to 0, which means the slice starts from the beginning of the list.
MCQ 4: What is the result of the following slicing operation: my_list[2:5]
?
- A) [2, 3, 4, 5]
- B) [3, 4]
- C) [2, 3, 4]
- D) [4, 5]
Answer: B) [3, 4]
- Explanation:
my_list[2:5]
extracts elements from index 2 (inclusive) to 5 (exclusive), resulting in [3, 4].
MCQ 5: How can you slice a list to get all elements from the beginning up to (but not including) index 3?
- A)
my_list[:3]
- B)
my_list[0:3]
- C)
my_list[1:3]
- D)
my_list[3:]
Answer: A) my_list[:3]
- Explanation:
my_list[:3]
extracts elements from the beginning of the list up to (but not including) index 3.
MCQ 6: Which of the following statements about slicing is true?
- A) Slicing modifies the original list.
- B) Slicing always includes the element at the end index.
- C) Slicing creates a new list with the sliced elements.
- D) Slicing requires specifying both start and end indices.
Answer: C) Slicing creates a new list with the sliced elements.
- Explanation: Slicing in Python creates a new list containing the sliced elements, leaving the original list unchanged.
MCQ 7: What is the result of the slicing operation: my_list[3:]
if my_list
contains 5 elements?
- A) All elements from index 0 to 3
- B) All elements from index 3 to 4
- C) All elements from index 3 to the end
- D) An empty list
Answer: C) All elements from index 3 to the end
- Explanation:
my_list[3:]
extracts elements starting from index 3 to the end of the list.