Skip to content
Home » Posts » Simple To-Do List Manager in Python

Simple To-Do List Manager in Python

  • by
  1. Displaying the To-Do List:
   # Function to display the to-do list
   def display_todo_list(todo_list):
       print("To-Do List:")
       for index, task in enumerate(todo_list, start=1):
           print(f"{index}. {task}")

This function takes a list (todo_list) as an argument and prints each task along with its index. It uses the enumerate function to get both the index and task.

  1. Adding a Task:
   # Function to add a task to the to-do list
   def add_task(todo_list, task):
       todo_list.append(task)
       print(f"Task '{task}' added to the to-do list.")

This function takes the to-do list (todo_list) and a new task as arguments, appends the task to the list, and prints a message indicating that the task has been added.

  1. Removing a Task:
   # Function to remove a task from the to-do list
   def remove_task(todo_list, task_number):
       if 1 <= task_number <= len(todo_list):
           removed_task = todo_list.pop(task_number - 1)
           print(f"Task '{removed_task}' removed from the to-do list.")
       else:
           print("Invalid task number. Please enter a valid task number.")

This function takes the to-do list (todo_list) and the index of the task to be removed (task_number). It checks if the provided index is valid, removes the task from the list using pop, and prints a message indicating that the task has been removed.

  1. Sample To-Do List and Usage:
   # Sample to-do list
   todo_list = ["Buy groceries", "Finish coding assignment", "Exercise"]

   # Display the initial to-do list
   display_todo_list(todo_list)

   # Add a new task
   add_task(todo_list, "Read a book")

   # Display the updated to-do list
   display_todo_list(todo_list)

   # Remove a task
   remove_task(todo_list, 2)

   # Display the final to-do list
   display_todo_list(todo_list)

In this section, a sample to-do list is created, and the functions are used to display, add, and remove tasks. You can modify this section to interact with the to-do list as per your requirements.