Building a Simple To-Do List Application in Python

 One of the most fundamental yet essential applications is a To-Do List. It’s perfect for helping you manage tasks, and building this app in Python will help you practice important programming concepts like user input, file handling, and conditionals. In this blog post, we will walk you through creating a simple To-Do List Application that allows users to add, delete, and view tasks.


Why Build a To-Do List App?

The To-Do List app is a classic beginner project for Python learners. It’s a great exercise in:

  • User Input Handling: You’ll learn how to take user input, validate it, and process it.
  • File Handling: You’ll save the tasks in a text file so that they persist between program runs.
  • Conditionals: Depending on the user's actions (like adding or deleting tasks), the program will execute different logic.
  • List Management: You’ll manage a collection of tasks and perform actions like adding, deleting, and listing them.


 

Setting Up the Project

First, we will need to decide on the features we want for our To-Do List application. These will include:

  1. Add a task
  2. View tasks
  3. Delete a task

Step 1: Create a Simple Python File

Create a Python file, for example, todo.py. In this file, you’ll implement the logic for adding, viewing, and deleting tasks.


Step 2: Writing the Code

Here’s a basic implementation of the To-Do List:


import os # Define file to store tasks task_file = "tasks.txt" # Function to read tasks from the file def read_tasks(): if os.path.exists(task_file): with open(task_file, "r") as file: tasks = file.readlines() return [task.strip() for task in tasks] else: return [] # Function to write tasks to the file def write_tasks(tasks): with open(task_file, "w") as file: for task in tasks: file.write(f"{task}\n") # Function to display the menu def show_menu(): print("\nTo-Do List Application") print("1. View Tasks") print("2. Add Task") print("3. Delete Task") print("4. Exit") # Function to add a new task def add_task(tasks): new_task = input("Enter the task you want to add: ") tasks.append(new_task) write_tasks(tasks) print(f"Task '{new_task}' added!") # Function to delete a task def delete_task(tasks): print("\nCurrent Tasks:") for index, task in enumerate(tasks, start=1): print(f"{index}. {task}") try: task_index = int(input("\nEnter the task number to delete: ")) - 1 if 0 <= task_index < len(tasks): deleted_task = tasks.pop(task_index) write_tasks(tasks) print(f"Task '{deleted_task}' deleted!") else: print("Invalid task number!") except ValueError: print("Please enter a valid number.") # Main function to run the app def main(): tasks = read_tasks() while True: show_menu() choice = input("\nChoose an option: ") if choice == "1": if tasks: print("\nYour Tasks:") for index, task in enumerate(tasks, start=1): print(f"{index}. {task}") else: print("No tasks available!") elif choice == "2": add_task(tasks) elif choice == "3": if tasks: delete_task(tasks) else: print("No tasks to delete!") elif choice == "4": print("Goodbye!") break else: print("Invalid choice! Please select a valid option.") if __name__ == "__main__": main()






Step 3: How It Works

  • Tasks File: Tasks are saved in a text file, tasks.txt. The app reads and writes tasks to this file every time you add or delete a task.
  • Menu: The app displays a simple menu with options to view tasks, add tasks, delete tasks, or exit the program.
  • Adding Tasks: When the user chooses to add a task, the app asks for input and appends the task to the list.
  • Deleting Tasks: The app displays a numbered list of current tasks and asks the user to input the number of the task they want to delete.



Step 4: Running the Program

To run your To-Do List application, follow these steps:

  1. Save the code above in a Python file (e.g., todo.py).
  2. Open your terminal or command prompt.
  3. Navigate to the folder containing the Python file.
  4. Run the program with the command:
    python todo.py
  5. Follow the on-screen prompts to add, delete, or view tasks.



Conclusion

This To-Do List Application serves as an excellent way to practice Python’s basic functionality. By working with file handling, lists, and user input, you’ll deepen your understanding of Python while building something practical and useful.

Feel free to expand this project by adding more features such as task prioritization, deadlines, or categories. You could also enhance it with a graphical user interface (GUI) using libraries like Tkinter or PyQt.



Leave a comment below if you have any questions or suggestions, and I’ll be happy to assist you further. Happy coding!