Python CLI Todo List Manager
Create a fully functional command-line Todo List manager in Python. Learn how to map functions to commands, manage arrays, and handle state.
How it Works
Command-line applications (CLI) form the backbone of developer tools.
This script maintains a list of tasks in memory and provides a menu approach to viewing, adding, and completing tasks.
The architecture mimics a primitive model-view-controller (MVC) pattern by separating the data (list) from the interaction (functions).
Source Code
A simple in-memory task manager demonstrating list mutations.
todo_app.py
Try in Editorclass TodoApp:
def __init__(self):
self.tasks = []
def add_task(self, task):
self.tasks.append({"title": task, "done": False})
print(f"Added: '{task}'")
def complete_task(self, index):
if 0 <= index < len(self.tasks):
self.tasks[index]["done"] = True
print(f"Completed: '{self.tasks[index]['title']}'")
def view_tasks(self):
print("\n--- Your Tasks ---")
for i, task in enumerate(self.tasks):
status = "[x]" if task["done"] else "[ ]"
print(f"{i}. {status} {task['title']}")
print("------------------\n")
app = TodoApp()
app.add_task("Learn Python basics")
app.add_task("Build a portfolio project")
app.view_tasks()
app.complete_task(0)
app.view_tasks()Terminal Output
Added: 'Learn Python basics'
Added: 'Build a portfolio project'
--- Your Tasks ---
0. [ ] Learn Python basics
1. [ ] Build a portfolio project
------------------
Completed: 'Learn Python basics'
--- Your Tasks ---
0. [x] Learn Python basics
1. [ ] Build a portfolio project
------------------Real-world Applications
- Task management utilities
- Object-Oriented state management exercises
- Building blocks for robust serverless functions
Frequently Asked Questions
How can I save the tasks permanently?
To persist data, you would write the self.tasks list to a file (like tasks.json) using Python's built-in `json` module, and load it during `__init__`.
More Examples
Python Fibonacci Sequence Generator
Run and understand the Fibonacci sequence in Python. This interactive code example shows iterative and recursive approaches to generating Fibonacci numbers.Python Sorting Algorithms
Explore python sorting algorithms. Visualize bubble sort and merge sort natively within a browser IDE context.