Fix the Slicing Bug
Intermediate · Fix the bug · from Python Slicing: Master Indexing for Strings, Lists, and Tuples
From the lesson: Python Slicing: Master Indexing for Strings, Lists, and Tuples
Imagine a pizza cut into 8 slices, numbered 0 through 7. You don't always want the whole pizza. Sometimes you want slices 2 through 5. Sometimes you want every other slice. Sometimes you want the last three slices.
Read the lessonThis code tries to get the last three items from a list, but it's using the wrong slice. Fix the bug so it prints ['date', 'elderberry', 'fig'].
fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry', 'fig']
# Bug: this doesn't get the last three items
last_three = fruits[3:0]
print(last_three)
More Slicing exercises
- Get the Middle ItemsBeginner
- Predict the Slice OutputBeginner
- Extract Even-Indexed ItemsIntermediate
- Reverse a WordBeginner
- Palindrome CheckerAdvanced
All Python exercises · 50 practice problems · Data Structures quiz