List slicing lets you access a portion of a list:
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4]) # [20, 30, 40]
print(numbers[:3]) # [10, 20, 30]
print(numbers[2:]) # [30, 40, 50]
The syntax is list[start:end] — the start is included, the end is excluded.
names = ['Charlie', 'Alice', 'Bob']
names.sort()
print(names) # ['Alice', 'Bob', 'Charlie']
📋 Instructions
Create a reading list:
```python
books = ['1984', 'Dune', 'Hamlet', 'Moby Dick', 'The Hobbit']
```
1. Print the first 3 books (use slicing)
2. Print the last 2 books (use slicing)
3. Sort the list and print it
Expected output:
```
['1984', 'Dune', 'Hamlet']
['Moby Dick', 'The Hobbit']
['1984', 'Dune', 'Hamlet', 'Moby Dick', 'The Hobbit']
```
Use books[:3] for first 3, books[-2:] or books[3:] for last 2, then books.sort() and print.
⚠️ Try solving it yourself first — you'll learn more!
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4]) # [20, 30, 40]
print(numbers[:3]) # [10, 20, 30]
print(numbers[2:]) # [30, 40, 50]