📘 Lesson 29 · Advanced
Project: Python To-Do App
Build a to-do app with add, complete, delete, and JSON persistence.
Project Overview
This project builds a command-line to-do list manager. Tasks are saved to a JSON file so they persist between sessions — closing the program doesn't lose your data. The project shows how different concepts combine in a real app: data structures, file I/O, functions, and user interaction.
Concepts: lists, dicts, JSON, file I/O, functions, while loop, enumerate, os.path.
Data Structure and Storage
Each task is a dictionary with a title and a done boolean. All tasks live
in a list. The load/save functions use JSON so data persists between runs. os.path.exists()
prevents a crash when the file hasn't been created yet.
storage.py
import json, os FILE = "tasks.json" def load_tasks(): if os.path.exists(FILE): with open(FILE) as f: return json.load(f) return [] def save_tasks(tasks): with open(FILE, "w") as f: json.dump(tasks, f, indent=2)
▶ Output
(Helper functions — no output)
Task Operations
ops.py
def show(tasks): if not tasks: print(" No tasks."); return for i, t in enumerate(tasks, 1): icon = "✓" if t["done"] else "○" print(f" {i}. [{icon}] {t['title']}") def add(tasks, title): tasks.append({{"title": title, "done": False}}) save_tasks(tasks); print(f"Added: {title}") def complete(tasks, i): tasks[i]["done"] = True save_tasks(tasks); print(f"Done: {tasks[i]['title']}") # Demo t = [] add(t, "Buy groceries") add(t, "Read Python docs") complete(t, 0) show(t)
▶ Output
Added: Buy groceries Added: Read Python docs Done: Buy groceries 1. [✓] Buy groceries 2. [○] Read Python docs
Extend this with priority levels, due dates, or categories — then build a GUI version with tkinter once you're comfortable.
🧠 Quick Check
Which built-in gives both index and value while looping?