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.
class 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()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
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Loops
Learn how to use Python loops to iterate over data. Master for loops, while loops, break, continue, and loop best practices with interactive examples.
How to Sort a List in Python
Learn how to sort a list in Python using the sort() method and the sorted() function. Discover custom key sorting and reverse order examples.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.