Exercises:
Exercise 1: Create a list of 5 different fruits and print the list.
- Hint: Use the
append()
method to add fruits to your list. - Solution:
fruits = [] fruits.append('Apple') fruits.append('Banana') fruits.append('Cherry') fruits.append('Date') fruits.append('Elderberry') print(fruits)
- Hint: Use the
Exercise 2: Add ‘Fig’ to the end of your fruit list and print the list.
- Hint: Use the
append()
method again. - Solution:
fruits.append('Fig') print(fruits)
- Hint: Use the
Exercise 3: Insert ‘Blueberry’ at the second position in your fruit list and print the list.
- Hint: Use the
insert()
method. - Solution:
fruits.insert(1, 'Blueberry') print(fruits)
- Hint: Use the
Exercise 4: Remove ‘Date’ from your fruit list and print the list.
- Hint: Use the
remove()
method. - Solution:
fruits.remove('Date') print(fruits)
- Hint: Use the
Exercise 5: Sort your fruit list in alphabetical order and print the list.
- Hint: Use the
sort()
method. - Solution:
fruits.sort() print(fruits)
- Hint: Use the