📦 Python Course in 4 to 6 Weeks – Lesson 8: Lists & Tuples in Python
Welcome to Lesson 8 of the Python Course in 4 to 6 Weeks! In this lesson, you'll learn about two essential sequence types in Python: Lists and Tuples. These structures allow you to store and organize multiple values in a single variable.
✅ What You'll Learn
- What are lists and tuples?
- How to create and use them
- Key differences between lists and tuples
- Common operations: indexing, slicing, looping
- Useful list methods and tuple features
🧺 What is a List?
A list is a mutable (changeable) sequence of values:
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple
fruits[1] = "orange" # Change value
print(fruits)
📎 What is a Tuple?
A tuple is similar to a list, but immutable (cannot be changed after creation):
colors = ("red", "green", "blue")
print(colors[2]) # Output: blue
# colors[1] = "yellow" # ❌ This will cause an error
🔍 Indexing and Slicing
numbers = [10, 20, 30, 40, 50]
print(numbers[0]) # First element
print(numbers[-1]) # Last element
print(numbers[1:4]) # Slice from index 1 to 3
🔁 Looping Through Sequences
for fruit in fruits:
print(fruit)
🧰 Useful List Methods
append()
– Add item to endremove()
– Remove item by valuepop()
– Remove item by indexsort()
– Sort items in placelen()
– Get number of items
📏 Key Differences
- ✅ Lists are mutable; tuples are immutable
- ✅ Lists use
[]
; tuples use()
- ✅ Lists are better for dynamic data; tuples for fixed collections
⚠️ Common Mistakes
- ❌ Trying to modify a tuple – use a list instead if you need changes
- ❌ Using the wrong brackets – don't mix up
[]
and()
- ❌ Forgetting that list methods don’t work on tuples
🧪 Practice Challenge
Create a Python file named shopping_cart.py
that:
- Starts with a list of items
- Lets the user add, remove, or view items
- Uses a tuple to show fixed prices for items
📥 Tools You'll Need
- 🐍 Python: https://www.python.org/downloads/
- 📝 VS Code: https://code.visualstudio.com/
- 🌐 Replit: https://replit.com/languages/python3
📌 Final Words
Lists and tuples help you store and manage collections of data efficiently. They’re foundational for everything from databases to game development. In the next lesson, we’ll dive into dictionaries and sets – perfect for handling key-value data and unique collections.
🔗 Coming Next:
Lesson 9: Dictionaries & Sets – Working with Mapped and Unique Data 🧩