📚 Lesson 8: Modules & Libraries
⏱️ Estimated time: 30 minutes | Difficulty: Intermediate
Standing on Shoulders
Modules allow you to use code written by others. Python's "Batteries Included" philosophy means it comes with a massive standard library for almost any task.
What is a List?
A list stores multiple values in one variable. Think of it like a shopping list!
Creating a List:
fruits = ["apple", "banana", "orange"]
numbers = [1, 2, 3, 4, 5]
mixed = ["Alice", 25, True, 3.14]
Accessing List Items
Lists use indexing (starting at 0):
fruits = ["apple", "banana", "orange"]
print(fruits[0]) # apple
print(fruits[1]) # banana
print(fruits[2]) # orange
print(fruits[-1]) # orange (last item)
Modifying Lists
Change Item
fruits[0] = "mango"
print(fruits)
# ['mango', 'banana', 'orange']
Add Item
fruits.append("grape")
print(fruits)
# [..., "grape"]
Remove Item
fruits.remove("banana")
print(fruits)
# ["apple", "orange"]
List Length
fruits = ["apple", "banana"]
print(len(fruits))
# 2
What is a Dictionary?
A dictionary stores data with labels (called keys). Instead of positions, you use meaningful names!
Creating a Dictionary:
student = {
"name": "Alice",
"age": 20,
"major": "Computer Science"
}
Accessing Dictionary Items
student = {"name": "Alice", "age": 20}
print(student["name"]) # Alice
print(student["age"]) # 20
Modifying Dictionaries
student = {"name": "Alice", "age": 20}
# Change a value
student["age"] = 21
# Add a new key
student["city"] = "New York"
# Remove a key
del student["city"]
print(student)
Output:
{'name': 'Alice', 'age': 21}
Real-World Example: Student Database
# List of dictionaries
students = [
{"name": "Alice", "grade": "A"},
{"name": "Bob", "grade": "B"},
{"name": "Charlie", "grade": "A"}
]
# Access data
for student in students:
print(student["name"], "got grade", student["grade"])
Output:
Alice got grade A
Bob got grade B
Charlie got grade A
✅ Quick Quiz
❓ What does this print?
fruits =
["apple", "banana", "orange"]
print(fruits[0])
❓ What is the main difference between lists and dictionaries?
❓ How do you add an item to a list?
🎯 Challenge - Create a Contact List
Write a program that:
- Creates a dictionary for 3 contacts (name, phone, email)
- Stores them in a list
- Loops through the list and prints each contact's info
Expected output format:
"Name: Alice, Phone: 555-1234, Email: alice@email.com"
🎉 Congratulations! 🎉
You've completed the Python tutorial! You now know:
- ✅ Variables and Data Types
- ✅ Math and Operations
- ✅ Making Decisions (If/Else)
- ✅ Loops and Iteration
- ✅ Functions and Reusable Code
- ✅ Lists and Dictionaries
Next step: Build your own projects and keep practicing! 🚀