To-do-list app (code) in python

← Back to Projects

๐Ÿ“ Python Console To-Do List App

Copy and paste this code into your Python editor or terminal to use:


todo_list = []

def show_menu():
    print("\n๐Ÿ“ To-Do List Menu")
    print("1. View Tasks")
    print("2. Add Task")
    print("3. Mark Task as Completed")
    print("4. Delete Task")
    print("5. Exit")

def view_tasks():
    if not todo_list:
        print("๐Ÿ“ญ No tasks in the list.")
    else:
        print("\nYour To-Do List:")
        for idx, task in enumerate(todo_list, start=1):
            status = "✅" if task['done'] else "❌"
            print(f"{idx}. {task['task']} [{status}]")

def add_task():
    task = input("✍️  Enter the task: ").strip()
    if task:
        todo_list.append({'task': task, 'done': False})
        print("✅ Task added!")
    else:
        print("⚠️  Empty task cannot be added.")

def mark_completed():
    view_tasks()
    if todo_list:
        try:
            task_num = int(input("✔️  Enter the task number to mark as completed: "))
            if 1 <= task_num <= len(todo_list):
                todo_list[task_num - 1]['done'] = True
                print("๐ŸŽ‰ Task marked as completed!")
            else:
                print("❌ Invalid task number.")
        except ValueError:
            print("⚠️  Please enter a valid number.")

def delete_task():
    view_tasks()
    if todo_list:
        try:
            task_num = int(input("๐Ÿ—‘️  Enter the task number to delete: "))
            if 1 <= task_num <= len(todo_list):
                removed = todo_list.pop(task_num - 1)
                print(f"๐Ÿ—‘️  Deleted task: {removed['task']}")
            else:
                print("❌ Invalid task number.")
        except ValueError:
            print("⚠️  Please enter a valid number.")

# Main loop
while True:
    show_menu()
    choice = input("๐Ÿ‘‰ Enter your choice (1-5): ").strip()

    if choice == '1':
        view_tasks()
    elif choice == '2':
        add_task()
    elif choice == '3':
        mark_completed()
    elif choice == '4':
        delete_task()
    elif choice == '5':
        print("๐Ÿ‘‹ Exiting To-Do List. Have a productive day!")
        break
    else:
        print("❌ Invalid choice. Please enter a number from 1 to 5.")


๐Ÿ“‹ Python To-Do List (Console) with File Saving

This version will save your tasks to todo.txt and loads them when you restart the app:


import os

FILE_NAME = "todo.txt"
todo_list = []

def load_tasks():
    if not os.path.exists(FILE_NAME):
        return
    with open(FILE_NAME, "r", encoding="utf-8") as file:
        for line in file:
            parts = line.strip().split("|")
            if len(parts) == 2:
                task, done = parts
                todo_list.append({'task': task, 'done': done == "1"})

def save_tasks():
    with open(FILE_NAME, "w", encoding="utf-8") as file:
        for item in todo_list:
            file.write(f"{item['task']}|{'1' if item['done'] else '0'}\n")

def show_menu():
    print("\n๐Ÿ“ To-Do List Menu")
    print("1. View Tasks")
    print("2. Add Task")
    print("3. Mark Task as Completed")
    print("4. Delete Task")
    print("5. Exit")

def view_tasks():
    if not todo_list:
        print("๐Ÿ“ญ No tasks in the list.")
    else:
        print("\nYour To-Do List:")
        for idx, task in enumerate(todo_list, start=1):
            status = "✅" if task['done'] else "❌"
            print(f"{idx}. {task['task']} [{status}]")

def add_task():
    task = input("✍️  Enter the task: ").strip()
    if task:
        todo_list.append({'task': task, 'done': False})
        save_tasks()
        print("✅ Task added!")
    else:
        print("⚠️  Empty task cannot be added.")

def mark_completed():
    view_tasks()
    if todo_list:
        try:
            task_num = int(input("✔️  Enter task number to mark completed: "))
            if 1 <= task_num <= len(todo_list):
                todo_list[task_num - 1]['done'] = True
                save_tasks()
                print("๐ŸŽ‰ Task marked as completed!")
            else:
                print("❌ Invalid task number.")
        except ValueError:
            print("⚠️  Please enter a valid number.")

def delete_task():
    view_tasks()
    if todo_list:
        try:
            task_num = int(input("๐Ÿ—‘️  Enter task number to delete: "))
            if 1 <= task_num <= len(todo_list):
                removed = todo_list.pop(task_num - 1)
                save_tasks()
                print(f"๐Ÿ—‘️  Deleted task: {removed['task']}")
            else:
                print("❌ Invalid task number.")
        except ValueError:
            print("⚠️  Please enter a valid number.")

# ๐Ÿ“ฆ Load tasks at startup
load_tasks()

# Main loop
while True:
    show_menu()
    choice = input("๐Ÿ‘‰ Enter your choice (1-5): ").strip()

    if choice == '1':
        view_tasks()
    elif choice == '2':
        add_task()
    elif choice == '3':
        mark_completed()
    elif choice == '4':
        delete_task()
    elif choice == '5':
        print("๐Ÿ‘‹ Exiting To-Do List. All changes saved!")
        break
    else:
        print("❌ Invalid choice. Please enter a number from 1 to 5.")


๐Ÿ“‹ Python GUI To-Do List with File Saving

This app lets users manage tasks in a GUI with automatic file saving to todo.txt:


import tkinter as tk
from tkinter import messagebox
import os

FILE_NAME = "todo.txt"

class ToDoApp:
    def __init__(self, root):
        self.root = root
        self.root.title("๐Ÿ“‹ To-Do List with File Saving")
        self.root.geometry("400x500")
        self.root.resizable(False, False)

        self.tasks = []

        self.entry = tk.Entry(root, font=("Arial", 14))
        self.entry.pack(padx=10, pady=10, fill=tk.X)

        self.add_button = tk.Button(root, text="➕ Add Task", font=("Arial", 12), bg="#4CAF50", fg="white", command=self.add_task)
        self.add_button.pack(padx=10, pady=5, fill=tk.X)

        self.task_listbox = tk.Listbox(root, font=("Arial", 12), selectmode=tk.SINGLE)
        self.task_listbox.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)

        self.complete_button = tk.Button(root, text="✅ Mark Completed", font=("Arial", 12), bg="#2196F3", fg="white", command=self.mark_completed)
        self.complete_button.pack(padx=10, pady=5, fill=tk.X)

        self.delete_button = tk.Button(root, text="๐Ÿ—‘️ Delete Task", font=("Arial", 12), bg="#f44336", fg="white", command=self.delete_task)
        self.delete_button.pack(padx=10, pady=5, fill=tk.X)

        self.load_tasks()

    def load_tasks(self):
        if not os.path.exists(FILE_NAME):
            return
        with open(FILE_NAME, "r", encoding="utf-8") as file:
            for line in file:
                task_text, done = line.strip().split("|")
                self.tasks.append({'task': task_text, 'done': done == "1"})
        self.refresh_list()

    def save_tasks(self):
        with open(FILE_NAME, "w", encoding="utf-8") as file:
            for task in self.tasks:
                file.write(f"{task['task']}|{'1' if task['done'] else '0'}\n")

    def refresh_list(self):
        self.task_listbox.delete(0, tk.END)
        for task in self.tasks:
            status = "✅" if task['done'] else "❌"
            self.task_listbox.insert(tk.END, f"{task['task']} [{status}]")

    def add_task(self):
        task_text = self.entry.get().strip()
        if not task_text:
            messagebox.showwarning("Empty Input", "Please enter a task.")
            return
        self.tasks.append({'task': task_text, 'done': False})
        self.entry.delete(0, tk.END)
        self.refresh_list()
        self.save_tasks()

    def mark_completed(self):
        index = self.task_listbox.curselection()
        if not index:
            messagebox.showwarning("No Selection", "Please select a task.")
            return
        self.tasks[index[0]]['done'] = True
        self.refresh_list()
        self.save_tasks()

    def delete_task(self):
        index = self.task_listbox.curselection()
        if not index:
            messagebox.showwarning("No Selection", "Please select a task.")
            return
        del self.tasks[index[0]]
        self.refresh_list()
        self.save_tasks()

# Start the app
if __name__ == "__main__":
    root = tk.Tk()
    app = ToDoApp(root)
    root.mainloop()