🐍 Lesson 7: Python Lists – Store Multiple Items in One Place
Welcome back! In this lesson, you’ll learn about lists — one of Python’s most commonly used data structures. Lists allow you to store multiple items in a single variable, making your programs more organized and efficient.
⭐ What You Will Learn in This Lesson
- What lists are and how they work
- Accessing and modifying list items
- Adding and removing items
- Common list operations like loops and membership checks
- Using lists to organize multiple values efficiently
👋 What Is a List?
A list is like a container that holds multiple items in a specific order. You can store numbers, strings, or even other lists inside a list.
fruits = ["apple", "banana", "cherry"]
print(fruits)
🧠 Output
['apple', 'banana', 'cherry']
💾 Accessing Items in a List
Use indexes to access specific items. Python uses zero-based indexing.
print(fruits[0]) # apple
print(fruits[1]) # banana
print(fruits[-1]) # cherry (last item)
🧠 Output
apple
banana
cherry
🛠 Modifying a List
You can change items using their index:
fruits[1] = "blueberry"
print(fruits) # ['apple', 'blueberry', 'cherry']
🧠 Output
['apple', 'blueberry', 'cherry']
➕ Adding Items to a List
- append() – adds to the end
- insert() – adds at a specific position
fruits.append("orange")
fruits.insert(1, "kiwi")
print(fruits) # ['apple', 'kiwi', 'blueberry', 'cherry', 'orange']
🧠 Output
['apple', 'kiwi', 'blueberry', 'cherry', 'orange']
➖ Removing Items from a List
- remove() – removes by value
- pop() – removes by index
- del – delete a specific index
fruits.remove("kiwi")
fruits.pop(1)
del fruits[0]
print(fruits)
🧠 Output
['blueberry', 'cherry', 'orange']
⚡ List Operations
- Length:
len(fruits) - Check if item exists:
"apple" in fruits - Loop through a list:
for fruit in fruits:
print(fruit)
🧠 Output
blueberry
cherry
orange
👥 Who Is This Lesson For?
- Beginners learning to manage multiple items in Python
- Students wanting to store and organize data efficiently
- Developers looking to use lists in real programs
❌ Common Mistakes
- Using the wrong index and getting an IndexError
- Trying to remove an item that doesn’t exist
- Mixing append() and insert() without understanding positions
- Modifying a list while looping through it incorrectly
❓ Frequently Asked Questions (FAQ)
1. Can a list contain different types of items?
Yes! A list can hold strings, numbers, or even other lists.
2. How do negative indexes work?
Negative indexes start counting from the end of the list, e.g., -1 is the last item.
3. What happens if I access an index that doesn’t exist?
You’ll get an IndexError. Always check the list length first.
4. Can I loop through a list in reverse?
Yes, using slicing like fruits[::-1] or the reversed() function.
🧪 Practice Exercises
- Create a list of your five favorite movies.
- Add one more movie to the list.
- Remove a movie you don’t like.
- Print each movie using a loop.
- Check if a specific movie is in your list.
fruits.sort() and then printing it.
🚀 What’s Next?
In the next lesson, you’ll learn about tuples, a similar data structure that is immutable. You’ll see how tuples differ from lists and when to use them.
Comments
Post a Comment