Chapter 5: Lists and Your First Project
Congratulations on reaching the final chapter of this beginner course! So far, you have learned about variables, conditional logic, loops, and functions. Now, we will introduce Lists—a powerful way to store collections of data—and then use everything you've learned to build a simple project.
1. What are Lists?
A list is a data structure that holds multiple items in a single variable. Lists are ordered, changeable, and can contain items of different types. You define a list using square brackets [].
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Accesses the first item: "apple"
Lists are incredibly useful for storing related data, such as a list of user names, high scores in a game, or a shopping list.
2. Manipulating Lists
Python makes it very easy to add, remove, or change items in a list using methods:
fruits.append("orange"): Adds an item to the end of the list.fruits.remove("banana"): Removes the first occurrence of "banana".len(fruits): Returns the number of items in the list.
3. Your First Project: A Simple To-Do List
Let's combine everything into a small program that allows a user to manage a to-do list. This program uses a loop to keep asking for tasks until the user types "done".
tasks = []
print("Enter your tasks (type 'done' to finish):")
while True:
task = input("> ")
if task == "done":
break
tasks.append(task)
print("Your To-Do List:")
for t in tasks:
print("- " + t)
Summary of Your Python Journey
You have now covered the essentials: installing Python, handling data, making logical decisions, automating tasks, and organizing code. Programming is a skill of practice. The best way to move from "beginner" to "intermediate" is to keep building small projects—try adding a feature to this To-Do list, like the ability to save tasks to a file or delete completed ones!
Try It Yourself
- Create a list of 5 of your favorite movies.
- Write a loop that prints each movie title with a number next to it (e.g., "1. Movie Name").
- Add a new movie to your list using the
.append()method and print the final list again.
Useful List Methods
Lists come with built-in methods that make managing collections of data much easier:
fruits = ["apple", "banana"]
fruits.append("cherry") # add to the end
fruits.insert(0, "mango") # add at a specific position
fruits.remove("banana") # remove by value
fruits.sort() # sort alphabetically
print(len(fruits)) # count items
List Comprehensions
List comprehensions let you build a new list from an existing one in a single, readable line — a very "Pythonic" way to transform data.
numbers = [1, 2, 3, 4, 5]
squares = [n * n for n in numbers]
print(squares) # [1, 4, 9, 16, 25]
evens = [n for n in numbers if n % 2 == 0]
print(evens) # [2, 4]
Once you're comfortable with loops, list comprehensions are a natural next step and something you'll see constantly in real-world Python code.
CodeMaster Academy