Python Todo List Program Code

In this source code example, we will write a simple Todo-List program in Python that performs basic operations such as add task, mark task done, view all tasks, etc.

Python Todo List Program Code

# To-Do List program
tasks = []

def display_menu():
    print("To-Do List")
    print("1. Add a task")
    print("2. Mark a task as done")
    print("3. View all tasks")
    print("4. Exit")

def add_task():
    task = input("Enter the task: ")
    tasks.append(task)
    print("Task added.")

def mark_as_done():
    task = input("Enter the task to mark as done: ")
    if task in tasks:
        tasks.remove(task)
        print("Task marked as done.")
    else:
        print("Task not found.")

def view_tasks():
    print("Tasks:")
    for i, task in enumerate(tasks):
        print(f"{i + 1}. {task}")

while True:
    display_menu()
    choice = int(input("Enter your choice: "))
    if choice == 1:
        add_task()
    elif choice == 2:
        mark_as_done()
    elif choice == 3:
        view_tasks()
    elif choice == 4:
        break
    else:
        print("Invalid choice.")

Output:

To-Do List
1. Add a task
2. Mark a task as done
3. View all tasks
4. Exit
Enter your choice: 1
Enter the task: learn python
Task added.
To-Do List
1. Add a task
2. Mark a task as done
3. View all tasks
4. Exit
Enter your choice: 1
Enter the task: learn java
Task added.
To-Do List
1. Add a task
2. Mark a task as done
3. View all tasks
4. Exit
Enter your choice: 2
Enter the task to mark as done: learn python
Task marked as done.
To-Do List
1. Add a task
2. Mark a task as done
3. View all tasks
4. Exit
Enter your choice: 2
Enter the task to mark as done: learn java
Task marked as done.
To-Do List
1. Add a task
2. Mark a task as done
3. View all tasks
4. Exit
Enter your choice: 3
Tasks:
To-Do List
1. Add a task
2. Mark a task as done
3. View all tasks
4. Exit
Enter your choice: 1
Enter the task: learn python frameworks
Task added.
To-Do List
1. Add a task
2. Mark a task as done
3. View all tasks
4. Exit
Enter your choice: 3
Tasks:
1. learn python frameworks
To-Do List
1. Add a task
2. Mark a task as done
3. View all tasks
4. Exit
Enter your choice: 4

Comments