Create a fully functional command-line To-Do List with Python
If you're learning Python and want to create something practical and fun, a To-Do List App is the perfect project.
In this guide, we'll build a fully functional command-line To-Do List that can add, view, mark, delete, and save tasks — all in Python!
Let's dive in
What You'll Learn
By the end of this tutorial, you'll understand:
- How to create and manage lists and dictionaries in Python
- How to save and load data using JSON files
- How to structure your Python code into functions
- How to handle user input and file operations
Step 1: Setup Your Python Environment
Before starting, make sure you have Python installed.
Check by running:
python --versionIf not installed, download it from python.org/downloads.
Now, open your favorite code editor (like VS Code or PyCharm) and create a new file named:
todo_app.pyStep 2: Import Required Modules
We'll use two built-in Python modules — json and os — for handling files and data.
import json
import osStep 3: Load and Save Tasks
We'll keep all tasks in a JSON file so they don't disappear when the program closes.
DATA_FILE = "tasks.json"
def load_tasks():
"""Load tasks from the JSON file if it exists."""
if os.path.exists(DATA_FILE):
try:
with open(DATA_FILE, "r") as f:
return json.load(f)
except json.JSONDecodeError:
print("⚠️ Error reading data file, starting fresh.")
return []
return []
def save_tasks(tasks):
"""Save tasks to the JSON file."""
with open(DATA_FILE, "w") as f:
json.dump(tasks, f, indent=4)✅ These two functions handle persistent storage of your tasks.
Step 4: Create the App Menu
Let's make a menu for the user to interact with:
def show_menu():
print("\n====== TO-DO LIST MENU ======")
print("1) Add a task")
print("2) View all tasks")
print("3) Mark a task as done")
print("4) Delete a task")
print("5) Exit and Save")
print("=============================")Step 5: Add, View, Complete, and Delete Tasks
Here's where the real action happens!
➕ Add a Task
def add_task(tasks):
title = input("\nType your new task: ").strip()
if title == "":
print("Oops! The task cannot be empty.")
return
tasks.append({"title": title, "done": False})
save_tasks(tasks)
print(f"✅ Added: {title}")View Tasks
def print_tasks(tasks):
if not tasks:
print("\nNo tasks yet. Add your first task!")
return
print("\nYour tasks:")
for i, task in enumerate(tasks, start=1):
status = "✓" if task["done"] else " "
print(f"{i}. [{status}] {task['title']}")Mark a Task as Done
def mark_task_done(tasks):
if not tasks:
print("\nThere are no tasks to mark. Add one first.")
return
print_tasks(tasks)
choice = input("\nEnter the task number to mark as done: ").strip()
try:
index = int(choice) - 1
if 0 <= index < len(tasks):
if tasks[index]["done"]:
print(f"Task already done: {tasks[index]['title']}")
else:
tasks[index]["done"] = True
save_tasks(tasks)
print(f"✅ Marked as done: {tasks[index]['title']}")
else:
print("That number is not in the list.")
except ValueError:
print("Please enter a valid number.")🗑 Delete a Task
def delete_task(tasks):
if not tasks:
print("\nThere are no tasks to delete. Add one first.")
return
print_tasks(tasks)
choice = input("\nEnter the task number to delete: ").strip()
try:
index = int(choice) - 1
if 0 <= index < len(tasks):
removed = tasks.pop(index)
save_tasks(tasks)
print(f"🗑️ Deleted: {removed['title']}")
else:
print("That number is not in the list.")
except ValueError:
print("Please enter a valid number.")Step 6: Put It All Together
Now we'll connect everything using a main loop:
def main():
tasks = load_tasks()
print("Welcome to the To-Do List App!")
while True:
show_menu()
choice = input("Choose an option (1-5): ").strip()
if choice == "1":
add_task(tasks)
elif choice == "2":
print_tasks(tasks)
elif choice == "3":
mark_task_done(tasks)
elif choice == "4":
delete_task(tasks)
elif choice == "5":
save_tasks(tasks)
print("\n💾 Tasks saved successfully. Goodbye!")
break
else:
print("Please choose a number from 1 to 5.")Finally, add this line so the program runs directly:
if __name__ == "__main__":
main()Step 7: Run the Program
Open your terminal in the project folder and type:
python todo_app.pyYou'll see:


