Module 17: Python ProjectsBeginner25 min read
Building a Command-Line Todo Application
Combine OOP, collections, and control flow into a complete working CLI tool.
Key Takeaways & Core Concepts
- Object-Oriented Programming (classes & methods).
- Collections (dictionaries & lists).
- Error handling and string formatting.
Building a Command-Line Todo Application
### Hands-on Project: Command-Line To-Do Application
In this project, we combine everything learned so far:
- Object-Oriented Programming (classes & methods).
- Collections (dictionaries & lists).
- Error handling and string formatting.
PYTHON Code Example
1class TodoList:
2 def __init__(self):
3 self.tasks = []
4
5 def add_task(self, title):
6 task = {"id": len(self.tasks) + 1, "title": title, "done": False}
7 self.tasks.append(task)
8 return f"Added: '{title}'"
9
10 def complete_task(self, task_id):
11 for task in self.tasks:
12 if task["id"] == task_id:
13 task["done"] = True
14 return f"Completed: '{task['title']}'"
15 return "Task not found."
16
17 def list_tasks(self):
18 if not self.tasks:
19 return "No tasks."
20 lines = []
21 for t in self.tasks:
22 status = "✓" if t["done"] else "○"
23 lines.append(f"[{status}] #{t['id']}: {t['title']}")
24 return "\n".join(lines)
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
24 lines
Running the Todo List
1class SimpleTodo:
2 def __init__(self):
3 self.tasks = []
4
5 def add(self, name):
6 self.tasks.append({"name": name, "done": False})
7
8 def mark_done(self, idx):
9 if 0 <= idx < len(self.tasks):
10 self.tasks[idx]["done"] = True
11
12 def report(self):
13 return [f"[{'X' if t['done'] else ' '}] {t['name']}" for t in self.tasks]
14
15app = SimpleTodo()
16app.add("Review Python PR on CodeZov")
17app.add("Write unit tests")
18app.mark_done(0)
19for item in app.report():
20 print(item)
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
20 lines
Note: Complete interactive task tracker running stateful logic.
Practice & AssessmentPython Programming Tutorial
Test Your Understanding
Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.
Your Current Score
0 / 0 correct(0 attempted)