Learn Python · lesson 45 of 60 · about 8 minutes
Capstone: Planning and Modelling
Plan Sam's habit tracker: decide what it does, what data it needs and how to model it.
What you'll learn
- Turn an idea into features
- Choose data structures
- Model data with a class
Sam's habit tracker
Sam wants the assistant to track habits like reading and walking: tick a habit off each day, and see how many days in a row it's been done. That's the capstone, built into the assistant over this unit. It starts with the model: a Habit class whose done days are a set, and a dict of habits looked up by name. Sam types the habits on one line with commas between them, so the assistant tidies each name, skips blanks and repeats, and says Tracking 2 habits: read, walk.
Start with a plan, not code
Your capstone is a habit tracker: you add habits like "read" or "walk", tick them off each day, and see your streaks.
Before coding, write the features as a short list:
1. Add a habit
2. Mark a habit done for a day
3. Show each habit's streak
4. Save and load everything from a file
Choosing the data
| Need | Structure |
|---|---|
| One habit's name and done days | A Habit object |
| The days it was done | A set of date strings (no duplicates) |
| All habits, looked up by name | A dict of name to Habit |
The model
class Habit:
def __init__(self, name):
self.name = name
self.done = set()
def mark(self, day):
self.done.add(day)
habits = {"read": Habit("read")}
habits["read"].mark("2026-05-01")
habits["read"].mark("2026-05-01")
print(len(habits["read"].done))Output
1Building the dict from one line
split(",") cuts the line at each comma, and strip() and lower() tidy each piece. Checking not in habits before adding means a habit typed twice is only made once, and the dict keeps the order the habits were first typed.
Habits from a line
habits = {}
for item in " Read, walk,, read ".split(","):
habit_name = item.strip().lower()
if habit_name != "" and habit_name not in habits:
habits[habit_name] = set()
print(list(habits))Output
['read', 'walk']Tip
Dates written as YYYY-MM-DD sort correctly as plain text, which keeps things simple.
Now try it yourself
This lesson is free in Codiqo, with 9 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.