Learn Python Lists and Loops: Build a Mini To-Do App!
Python Lists, Loops & Simple Projects: Next Steps for Beginners
Welcome back to TECH WORLD! In this tutorial, we're taking the next step in your Python journey. You’ll learn how to work with lists, loop through them, and create a mini To-Do app using everything we've covered so far.
1. What is a List in Python?
A list stores multiple items in a single variable.
fruits = ["apple", "banana", "cherry"]
Key points:
- Lists are written in square brackets
[]
. - Items are separated by commas.
- They can contain strings, numbers, or even other lists!
2. Accessing Items in a List
print(fruits[0]) # apple
print(fruits[2]) # cherry
List indexes start at 0. So fruits[0]
is the first item.
3. Looping Through a List
Use a for
loop to go through each item in a list.
for fruit in fruits:
print("I like", fruit)
This prints each fruit with a custom message.
4. Adding and Removing Items
fruits.append("orange") # Adds to the end
fruits.remove("banana") # Removes the first match
Common list methods:
.append()
- Add a new item.remove()
- Remove a specific itemlen(list)
- Count number of items
5. Using Functions with Lists
Let’s create a function that shows all items in a list:
def show_list(items):
for item in items:
print("- " + item)
tasks = ["study", "code", "exercise"]
show_list(tasks)
6. Mini Project: Simple To-Do App
Try this complete example:
tasks = []
def add_task(task):
tasks.append(task)
def show_tasks():
print("Your Tasks:")
for i, task in enumerate(tasks):
print(str(i + 1) + ".", task)
# Program starts
add_task("Finish Python tutorial")
add_task("Start project")
show_tasks()
Output:
Your Tasks:
1. Finish Python tutorial
2. Start project
You can now add tasks and display them using simple functions and loops!
Tech World
Edit and run Python code right here:
Press ▶ to run, or try editing the code!
What’s Next?
- Learn about if-elif-else for more conditions
- Create an interactive to-do app using
input()
- Dive into functions that return values
Keep following TECH WORLD as we continue building your Python skills, one line at a time!
Comments
Post a Comment