Code

Tides and Timetables

The datetime module understands calendars, so you don't have to remember how many days are in each month.

from datetime import date, datetime, timedelta

festival = date(2026, 12, 31)
today = date.fromisoformat("2026-11-25")   # "YYYY-MM-DD"
gap = festival - today                     # a timedelta
print(gap.days)                            # 36

print(today + timedelta(days=10))          # 2026-12-05
print(today.strftime("%A"))                # Wednesday

datetime adds a time of day. strptime parses text into a datetime and strftime formats one back into text:

depart = datetime.strptime("2026-06-01 22:30", "%Y-%m-%d %H:%M")
arrive = depart + timedelta(hours=5)
print(arrive.strftime("%Y-%m-%d %H:%M"))   # 2026-06-02 03:30

Common codes: %Y year, %m month, %d day, %H hour (24h), %M minute, %A weekday name.

Pip tip: date.today() and datetime.now() change every day, so tests use fixed dates instead.

Assignment

  1. days_until(today, event): both are "YYYY-MM-DD" strings. Return the number of days from today to event as an int.
  2. arrival_time(depart, hours): depart looks like "2026-06-01 22:30". Add hours (may be a decimal like 1.5) and return the result in the same format.
  3. weekday_name(day): return the weekday of a "YYYY-MM-DD" string, like "Wednesday".