Learn to build a console-based To-Do List Manager in Python with this step-by-step tutorial. Covers task addition, viewing, deletion, and exit functionality using lists and loops.
Introduction: Python To-Do List Manager
In this article, we will deconstruct a simple yet functional To-Do List Manager built in Python. You will learn how to create an interactive console application that allows users to add new tasks, view all existing tasks, delete specific tasks, and exit the program gracefully.
We will break down the code step-by-step, explaining the use of lists, while loops, if statements, and list methods like append() and pop(). By the end of this guide, you will have a solid understanding of the logic behind building a basic command-line application and be able to extend it with your own features.
WATCH VIDEO: Python Beginner Project Build a To-Do List Manager Step by Step
Complete Code: Python To-Do List Manager
def to_do_list():
#list contains all task
tasks = []
while True:
#Tasks menu options
print("-+-+-+-+- To-Do List Manager -+-+-+-+-\n")
print("1. Add a new task")
print("2. View all tasks")
print("3. Delete a task")
print("4. Exit the program\n")
#ask user to enter a 'task number' to initiate the program
ask_user = input("Enter the 'task number' to select task: ")
#add a new task
if ask_user == "1":
enter_task = input("Enter the task: ")
add_task = tasks.append(enter_task)
print(f"Task: '{enter_task}' is added successfully.")
print()
#View all tasks
if ask_user == "2":
#task list is empty
if not tasks:
print("Task list is empty.")
print()
else:
#list all tasks with its assigned number
print("Here are the tasks:")
for number, add_task in enumerate(tasks, 1):
print(f"{number}.{add_task}")
print()
#Delete a task
if ask_user == "3":
if not tasks:
print("Task list is empty.")
print()
else:
#delete task if available via index number
print("Here are the tasks available to delete.")
for number, add_task in enumerate(tasks, 1):
print(f"{number}.{add_task}")
task_number = int(input("Enter the task number to delete: "))
deleted_tasks = tasks.pop(task_number-1)
print()
print(f"Task: '{deleted_tasks}' is deleted successfully.")
print()
#Exit the program
if ask_user == "4":
print("You're exit now.")
break
#call the function to start the program
to_do_list()
Logic Building Steps:
- Add a new task
- View all tasks
- Delete a task
- Exit the program
Step-by-Step Code Explanation:
Let's walk through the code block by block to understand how our To-Do List Manager works.
1. The Function and Task Storage
def to_do_list():
tasks = []
- def to_do_list():: This line defines a function named to_do_list that encapsulates all the logic of our application. Using a function makes the code organized and reusable.
- tasks = []: Inside the function, we initialize an empty list called tasks. This list will act as our in-memory database, storing all the tasks the user enters for the duration of the program's run.
2. The Main Program Loop
while True:
print("-+-+-+-+- To-Do List Manager -+-+-+-+-\n")
print("1. Add a new task")
print("2. View all tasks")
print("3. Delete a task")
print("4. Exit the program\n")
ask_user = input("Enter the 'task number' to select task: ")
- while True:: This creates an infinite loop, ensuring our program keeps running until the user explicitly chooses to exit. The menu will be displayed repeatedly after each action.
- Printing the Menu: The print statements display a user-friendly menu with four numbered options.
- ask_user = input(...): This captures the user's choice as a string. The user enters '1', '2', '3', or '4' to select an action.
3. Adding a New Task (Option 1)
if ask_user == "1":
enter_task = input("Enter the task: ")
add_task = tasks.append(enter_task)
print(f"Task: '{enter_task}' is added successfully.")
print()
- Condition Check: if ask_user == "1": checks if the user wants to add a task.
- Getting Task Input: input("Enter the task: ") prompts the user to type their task, which is stored in the enter_task variable.
- Storing the Task: tasks.append(enter_task) adds the new task to the end of our tasks list.
- User Feedback: A confirmation message is printed using an f-string, and an empty print() adds a blank line for better readability.
4. Viewing All Tasks (Option 2)
if ask_user == "2":
if not tasks:
print("Task list is empty.")
print()
else:
print("Here are the tasks:")
for number, add_task in enumerate(tasks, 1):
print(f"{number}.{add_task}")
print()
- Condition Check: if ask_user == "2": triggers the view-all-tasks functionality.
- Checking for Empty List: if not tasks: is a efficient way to check if the list is empty. If it is, it informs the user.
- Enumerating the List: If there are tasks, the enumerate(tasks, 1) function is used. This loops over the list while keeping a counter. The 1 specifies that the numbering should start from 1 instead of the default 0.
- Formatted Output: print(f"{number}.{add_task}") prints each task with a number in front of it, making the list easy to read.
5. Deleting a Task (Option 3)
if ask_user == "3":
if not tasks:
print("Task list is empty.")
print()
else:
print("Here are the tasks available to delete.")
for number, add_task in enumerate(tasks, 1):
print(f"{number}.{add_task}")
task_number = int(input("Enter the task number to delete: "))
deleted_tasks = tasks.pop(task_number-1)
print()
print(f"Task: '{deleted_tasks}' is deleted successfully.")
print()
- Condition and Empty Check: Similar to the view option, it first checks if the user selected '3' and then if the list is empty.
- Display Tasks for Deletion: It shows the numbered list of tasks so the user knows which number to enter for deletion.
- Getting the Task Number to Delete: int(input(...)) captures the user's input and converts it from a string to an integer.
- The pop() Method: tasks.pop(task_number-1) is the key here. The pop() method removes and returns the item at the given index.
- Why task_number-1? Lists are zero-indexed (the first item is at index 0), but we displayed the list starting from 1. Subtracting 1 converts the user's input (1-based) to the correct list index (0-based).
- Confirmation: The task that was pop()ped is stored in deleted_tasks, and a confirmation message is printed.
6. Exiting the Program (Option 4)
if ask_user == "4":
print("You're exit now.")
break
- Condition Check: if ask_user == "4": handles the exit command.
- break Statement: This is the command that breaks the while True infinite loop, allowing the program to end gracefully.
7. Starting the Program
to_do_list()
This final line calls the function we defined, actually starting the To-Do List Manager when the script is run.
Conclusion: Python To-Do List Manager
Congratulations! You have successfully built a foundational To-Do List Manager in Python. This project demonstrates core programming concepts such as control flow with loops and conditionals, data management with lists, and basic user interaction through the console.
While this application is simple, it provides a perfect starting point for further enhancements. You could consider adding features like editing existing tasks, setting due dates, categorizing tasks, or saving the task list to a file so it persists after the program closes.
EXPLORE MORE PYTHON BEGINNER PROJECTS

Top comments (0)