π Quick Recap (Day 7)
You learned how to repeat tasks with
forandwhileloops.You used
breakto stop loops early andcontinueto skip parts of loops.
π― What Youβll Learn Today
What a list is and why itβs useful.
How to create a list in Python.
How to access and modify items by their position.
Common list methods:
append(),insert(),pop(), andremove().
π What Is a List?
Imagine a school locker with numbered shelves, where you store books in order. A list is like that locker: an ordered collection where each item has a number (called an index) starting from 0.
Ordered: items keep the order you add them.
Mutable: you can add, remove, or change items anytime.
Creating a List
Use square brackets [] and put items inside, separated by commas:
fruits = ["apple", "banana", "cherry"]
print(fruits)Output:
['apple', 'banana', 'cherry']π Accessing Items by Index
Each itemβs position is its index:
First item is index 0.
Second is index 1, and so on.
You can also use negative indices to count from the end: -1 is the last item.
print(fruits[0]) # apple
print(fruits[2]) # cherry
print(fruits[-1]) # cherryπ Changing a List
Adding Items
append(value): addvalueat the end.insert(index, value): addvalueat a specific index.
fruits.append("date")
fruits.insert(1, "blueberry")
print(fruits)Output:
['apple', 'blueberry', 'banana', 'cherry', 'date']Removing Items
pop(): removes and returns the last item (orpop(index)for a specific index).remove(value): removes the first occurrence ofvalue.
last = fruits.pop()
fruits.remove("banana")
print(last)
print(fruits)Output:
date
['apple', 'blueberry', 'cherry']Changing Items
Assign a new value to an existing index:
fruits[0] = "apricot"
print(fruits)Output:
['apricot', 'blueberry', 'cherry']π§ββοΈ Take the Wand and Try Yourself
Create a file named
list_practice.py.Inside it, create a list
bookswith 5 of your favorite book titles.Print:
The first book (
books[0]).The last book (
books[-1]).
Use
append()to add one more book.Use
insert()to add a book at index 2.Use
pop(3)to remove the fourth book and print which title was removed.Change the second bookβs title by assigning to
books[1].Print the final
bookslist.
Solution Example (list_practice.py):
# list_practice.py
books = [
"1984",
"To Kill a Mockingbird",
"The Hobbit",
"Pride and Prejudice",
"The Catcher in the Rye"
]
# Print first and last
print("First book:", books[0])
print("Last book:", books[-1])
# Add and insert
books.append("Moby Dick")
books.insert(2, "Brave New World")
# Remove and show removed
removed_book = books.pop(3)
print("Removed book:", removed_book)
# Modify second item
books[1] = "Animal Farm"
# Final list
print("Final books list:", books)Run:
python list_practice.pyExpected output:
First book: 1984
Last book: The Catcher in the Rye
Removed book: The Hobbit
Final books list: ['1984', 'Animal Farm', 'Brave New World', 'Pride and Prejudice', 'The Catcher in the Rye', 'Moby Dick']Once your script prints the expected results, youβve mastered Python lists!
Ready for Day 9: Tuples? Letβs go!