πŸ”„ Quick Recap (Day 7)

  • You learned how to repeat tasks with for and while loops.

  • You used break to stop loops early and continue to skip parts of loops.

🎯 What You’ll Learn Today

  1. What a list is and why it’s useful.

  2. How to create a list in Python.

  3. How to access and modify items by their position.

  4. Common list methods: append(), insert(), pop(), and remove().

πŸ“– 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): add value at the end.

  • insert(index, value): add value at 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 (or pop(index) for a specific index).

  • remove(value): removes the first occurrence of value.

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

  1. Create a file named list_practice.py.

  2. Inside it, create a list books with 5 of your favorite book titles.

  3. Print:

    • The first book (books[0]).

    • The last book (books[-1]).

  4. Use append() to add one more book.

  5. Use insert() to add a book at index 2.

  6. Use pop(3) to remove the fourth book and print which title was removed.

  7. Change the second book’s title by assigning to books[1].

  8. Print the final books list.

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.py

Expected 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!

Keep Reading