Sub topic 3
Recording a check-in
This chapter assumes you know basic Python (functions, dictionaries, sets, loops) and the date type from the datetime module. You will write the function that records one check-in. Every later calculation reads the data this function produces, so it has to be correct.
What a check-in is
A check-in says "this habit was done on this calendar day." The previous chapter modeled the data as a dictionary that maps each habit name to a set of dates. A check-in adds one date to one habit's set.
The check-in carries no time of day and no count. A habit is either done on a day or it is not. This keeps the later streak and completion-rate calculations simple.
The check-in function
from datetime import date
log = {} # habit name -> set of dates on which it was done
def check_in(habit, day=None):
day = day or date.today()
log.setdefault(habit, set()).add(day)
check_in("read")
check_in("read") # a duplicate check-in on the same day changes nothing
print(len(log["read"])) # 1The function has three parts.
- A default day. If you pass no
day, the function usesdate.today(). Passing a day lets you log a past date, for example when you forgot to check in yesterday. It also lets tests use fixed dates. - Creating the set when needed.
log.setdefault(habit, set())returns the habit's existing set. If the habit is new, it first stores an empty set and returns that. This removes the need for anif habit not in logbranch. - Adding the date.
.add(day)puts the date in the set.
Why a set makes duplicates harmless
A set holds each value at most once. Adding a date that is already present does nothing, so the function is idempotent. Calling it twice has the same effect as calling it once.
This matters in practice. People run the command twice, a script retries, or a habit gets logged from two places. If you stored check-ins in a list, each repeat would add another entry. You would then have to remove duplicates before every calculation, and forgetting to would inflate the numbers. With a set, the data cannot hold duplicates in the first place.
What happens inside the call
Common errors
- Using a
datetimeinstead of adate.datetime.now()includes the time. Two check-ins on the same day then become two different values, and the set keeps both. Always usedate.today()or adateobject. - Passing a string.
check_in("read", "2026-11-27")stores a string. Later date arithmetic fails, or a string and adatefor the same day sit side by side as two entries. Convert withdate.fromisoformat("2026-11-27")first. - A mutable default argument. Writing
def check_in(habit, day=date.today())evaluatesdate.today()once, when the function is defined. A program that runs past midnight would keep logging the old date. UsingNoneand resolving it inside the function avoids this. - Different spellings of a habit name.
"Read"and"read"are two habits. Pick one form, for example lowercase, and apply it in one place if you want to be strict.
Checking your work
You can confirm the behavior with a short test that uses fixed dates:
from datetime import date
log = {}
def check_in(habit, day=None):
day = day or date.today()
log.setdefault(habit, set()).add(day)
check_in("read", date(2026, 11, 25))
check_in("read", date(2026, 11, 26))
check_in("read", date(2026, 11, 26)) # duplicate
assert log["read"] == {date(2026, 11, 25), date(2026, 11, 26)}
print(sorted(log["read"]))The assertion passes because the duplicate was absorbed. Fixed dates make the result the same on every run, unlike date.today(). You will use the same approach to test streaks in the next chapters.
Exercise
A teammate changes the log so that each habit stores a list of dates, and check_in uses append. Running check_in("read") twice on the same day now gives len(log["read"]) == 2. What is the problem for later calculations, and how do you fix it while keeping the list?
Show answer
A list keeps duplicates, so the same day is counted twice. Completion rate would count it twice and could exceed 100%, and the data no longer represents "done or not done" for a day. The simplest fix is to go back to a set. If the list must stay, check if day not in log[habit] before appending, or convert to set(...) before every calculation. The set is better because it makes duplicates impossible instead of relying on every caller to remember.
Exercise
Why does check_in use day=None and then day = day or date.today() instead of day=date.today() in the signature?
Show answer
A default in the signature is evaluated once, when the function is defined. A long-running program would keep using the date from when it started, so check-ins after midnight would be logged on the wrong day. Resolving None inside the function runs date.today() on every call.