Store, Repeat, Automate
🧭 Introduction
In the previous lesson, you learned how to use variables to store single values.
But in real life, we deal with lists of things: shopping items, tasks, names, scores.
And to process multiple items efficiently, we use lists and loops.
In this lesson you’ll learn how to:
- Create and modify lists
- Use loops to process items automatically
- Build a simple interactive To-Do List app
📦 1. Lists in Python
✅ Basic Example
shopping = ["Milk", "Bread", "Eggs", "Water"]
print(shopping)
💬 Output:
['Milk', 'Bread', 'Eggs', 'Water']
A list is an ordered, editable container that holds multiple values.
🔍 Access by Index
print(shopping[0]) # First item
print(shopping[2]) # Third item
print(shopping[-1]) # Last item
💬 Output:
Milk
Eggs
Water
Indexes start at 0. Negative indexes count from the end.
🧠 2. Modifying a List
➕ Add Items
shopping.append("Cookies")
print(shopping)
💬 Output:
['Milk', 'Bread', 'Eggs', 'Water', 'Cookies']
➖ Remove Items
shopping.remove("Bread")
print(shopping)
💬 Output:
['Milk', 'Eggs', 'Water', 'Cookies']
🧨 Pop the Last Item
last = shopping.pop()
print("Removed:", last)
print(shopping)
💬 Output:
Removed: Cookies
['Milk', 'Eggs', 'Water']
🧮 Count Items
print("You have", len(shopping), "items.")
💬 Output:
You have 3 items.
🔁 3. Loops: Process Each Item Automatically
🧪 Basic Loop
for item in shopping:
print("✔️", item)
💬 Output:
✔️ Milk
✔️ Eggs
✔️ Water
🔢 Enumerate with Index
for i, item in enumerate(shopping, start=1):
print(f"{i}) {item}")
💬 Output:
1) Milk
2) Eggs
3) Water
🧰 4. Mini App — Interactive To-Do List
🎯 Goal:
Create an interactive program that builds a to-do list from user input.
💻 Code:
todo = []
print("Welcome! Enter your tasks. Type 'done' to finish.")
while True:
task = input("New task: ")
if task.lower() == "done":
break
todo.append(task)
print("\n📋 Your To-Do List:")
for i, task in enumerate(todo, start=1):
print(f"{i}. {task}")
💬 Example Output:
Welcome! Enter your tasks. Type 'done' to finish.
New task: Study Python
New task: Buy milk
New task: Gym
New task: done
📋 Your To-Do List:
1. Study Python
2. Buy milk
3. Gym
🧪 5. Practice Challenge — Average & Maximum
🧠 Description:
Write a program that:
- Asks the user for 5 numbers
- Saves them in a list
- Prints the average and maximum value
💻 Code:
numbers = []
for i in range(5):
n = float(input(f"Enter number {i+1}: "))
numbers.append(n)
average = sum(numbers) / len(numbers)
maximum = max(numbers)
print("\nNumbers:", numbers)
print("Average:", average)
print("Maximum:", maximum)
💬 Sample Output:
Enter number 1: 10
Enter number 2: 7.5
Enter number 3: 9
Enter number 4: 4
Enter number 5: 12
Numbers: [10.0, 7.5, 9.0, 4.0, 12.0]
Average: 8.5
Maximum: 12.0
🎯 What You Learned
- How to use lists to store multiple values
- How to loop through lists using
forandenumerate - How to build real programs that interact with the user
