python · level 2 · unit 5 · lesson 3
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A holds a day, and subtracting dates counts the days.
imports two things by name, so you can write date instead of datetime.date. date(2026, 12, 25) is Christmas Day 2026: year, month, then day.
Subtract one date from another and you get a , a length of time. Its .days says how many days apart they are. Add timedelta(days=7) to a date and you land a week later.
turns a date into text: %d is the day, %m the month number, %B the month name, %Y the year and %A the weekday.
from datetime import date, timedelta
today = date(2026, 11, 2)
christmas = date(2026, 12, 25)
wait = christmas - today
print(f"{wait.days} days to go")
print(today + timedelta(days=7))
print(christmas.strftime("%A %d %B %Y")) Lessons use fixed dates, because date.today() gives a different answer every day.
guess first
A library book is due back. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
from datetime import date, timedelta
borrowed = date(2026, 2, 20)
due = borrowed + timedelta(days=14)
print(due)
print(due.strftime("%d/%m/%Y"))
your turn
Your turn. Count down to your birthday.
Type today's date and your next birthday, each as year, month, day with commas. Then fill in the attribute that gives the number of days.
fix it
The exam countdown goes backwards. Can you fix it?
- The answer is negative. Which date is later, and which one comes first in the subtraction?
- Like with numbers, put the later date first: later minus earlier gives a positive answer.
- Change line 4 to left = exam - today, then run.
nailed it
you made time work for you. :D
from datetime import dateimports one thing by name.- Later date minus earlier date gives a timedelta with
.days. strftimeturns a date into text, like%d/%m/%Y.
new words in your collection
- from
- date
- timedelta
- strftime
This lesson isn’t saved. A free account remembers every lesson you finish.