
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()anddatetime.now()change every day, so tests use fixed dates instead.
Assignment
days_until(today, event): both are"YYYY-MM-DD"strings. Return the number of days fromtodaytoeventas anint.arrival_time(depart, hours):departlooks like"2026-06-01 22:30". Addhours(may be a decimal like1.5) and return the result in the same format.weekday_name(day): return the weekday of a"YYYY-MM-DD"string, like"Wednesday".
days_until(today, event): both are"YYYY-MM-DD"strings. Return the number of days fromtodaytoeventas anint.arrival_time(depart, hours):departlooks like"2026-06-01 22:30". Addhours(may be a decimal like1.5) and return the result in the same format.weekday_name(day): return the weekday of a"YYYY-MM-DD"string, like"Wednesday".
main.py Python sleeping
Press Run to try your code, then Check to test it.