Slicing a list in Python allows us to extract a portion of the list using a specified range of indices. Here are two examples:
Example 1:
numbers = [1, 2, 3, 4, 5] sliced_numbers = numbers[2:4] print(sliced_numbers)
Output:
[3, 4]
Example 2:
fruits = ['apple', 'banana', 'orange', 'kiwi', 'melon'] sliced_fruits = fruits[1:3] print(sliced_fruits)
Output:
[‘banana’, ‘orange’]