PYTHON PROJECT 13

Python To-Do List

Learn how to build a simple To-Do List using Python lists, functions, loops, conditions and user input.

1. Project Introduction

A To-Do List helps users keep track of tasks that need to be completed.

In this project, we will create a program where users can add tasks, view tasks, mark tasks as completed and delete tasks.

This project is designed to practice Python programming fundamentals.

2. Creating the Task List

A Python list can store multiple tasks.

tasks = []

3. Adding a Task

The append() method adds a new item to a list.

task = input(
    "Enter a task: "
)

tasks.append(task)

4. Creating an Add Task Function

def add_task():

    task = input(
        "Enter a task: "
    )

    if task:

        tasks.append(task)

        print("Task added.")

5. Viewing Tasks

A for loop can be used to display every task.

for index, task in enumerate(tasks, start=1):

    print(
        index,
        task
    )

6. Using enumerate()

enumerate() gives us both the index and the value.

tasks = [
    "Learn Python",
    "Practice Coding",
    "Build Project"
]

for number, task in enumerate(
    tasks,
    start=1
):

    print(
        f"{number}. {task}"
    )

7. Completing a Task

We can store each task as a dictionary so that it can contain a completion status.

task = {
    "title": "Learn Python",
    "completed": False
}

8. Changing Task Status

When a task is completed, its status can be changed from False to True.

task["completed"] = True

9. Creating a Task Function

def create_task(title):

    return {
        "title": title,
        "completed": False
    }

10. Adding Dictionary Tasks

task = create_task(
    "Learn Python"
)

tasks.append(task)

11. Displaying Task Status

for task in tasks:

    if task["completed"]:

        print(
            "✓",
            task["title"]
        )

    else:

        print(
            "○",
            task["title"]
        )

12. Marking a Task Complete

task_number = int(
    input(
        "Enter task number: "
    )
)

index = task_number - 1

if 0 <= index < len(tasks):

    tasks[index]["completed"] = True

    print("Task completed.")

13. Deleting a Task

The pop() method can remove a task from the list.

index = task_number - 1

if 0 <= index < len(tasks):

    tasks.pop(index)

    print("Task deleted.")

14. Creating a Delete Function

def delete_task():

    number = int(
        input(
            "Enter task number: "
        )
    )

    index = number - 1

    if 0 <= index < len(tasks):

        tasks.pop(index)

        print("Task deleted.")

    else:

        print("Invalid task number.")

15. Counting Completed Tasks

completed = 0

for task in tasks:

    if task["completed"]:

        completed += 1

print(
    "Completed:",
    completed
)

16. Counting Pending Tasks

pending = 0

for task in tasks:

    if not task["completed"]:

        pending += 1

print(
    "Pending:",
    pending
)

17. Creating the Menu

while True:

    print()
    print("1. Add Task")
    print("2. View Tasks")
    print("3. Complete Task")
    print("4. Delete Task")
    print("5. Exit")

    choice = input(
        "Enter choice: "
    )

18. Using if / elif

if choice == "1":

    add_task()

elif choice == "2":

    view_tasks()

elif choice == "3":

    complete_task()

elif choice == "4":

    delete_task()

elif choice == "5":

    break

else:

    print(
        "Invalid choice."
    )

19. Input Validation

User input should be checked before changing the task list.

if not task:

    print(
        "Task cannot be empty."
    )

elif task_number < 1:

    print(
        "Invalid task number."
    )

20. Complete Project Flow

01 Create Task List
02 Add Task
03 View Tasks
04 Complete Task
05 Delete Task
06 Count Tasks
07 Validate Input
08 Exit Program

21. Concepts You Practiced

22. Run Complete Project 🚀

You have learned how the To-Do List works. Now open the complete interactive project.

Run Complete Project