Your report says the month you typed, and it will still say it when the month is over. This lesson has your tracker read today's date instead, using code that comes with Python. It also counts the days left in the month.
The concept
Here is your tracker as the last lesson left it. Run it:
Look at the line under your title. You typed that month by hand. Now run this:
Your console shows today's date, written year first. Under it are the year, the month and the day, each as a whole number. Run it tomorrow and the day changes.
You never wrote any code that knows the date. It comes with Python, inside a module: a file of ready-made code that you can bring into your program. Python comes with hundreds of them.
The first line, from datetime import date, is an import. It finds the module called datetime and hands you one name from it: date. Then date.today() gives back today's date, and today stores it.
A date carries its parts with it. You read them with a dot: today.year, today.month and today.day. No parentheses follow them. They are values the date holds, not functions to call.
There is a second shape of import. Run this:
This time the import brings in the whole calendar module, under its own name. You reach inside it with a dot: calendar.monthrange. Give it a year and a month, and it gives back two values together, in parentheses.
The first is the weekday the month starts on. Python shows it by name, calendar.SUNDAY, because November 2026 starts on a Sunday. The second is how many days the month has. [1] picks the second, the same way it picks the second item of a list.
One more piece. Your report shows the month as a word, not as a number. Run this:
The first line is today's month and year. The second adds the day in front. strftime turns a date into text, following a pattern you give it. Each code that starts with % is replaced by part of the date:
%Bis the month's full name, such asNovember.%Yis the year, with four digits.%dis the day of the month, always two digits, so the 5th shows as05.
Everything else in the pattern, such as the spaces, stays exactly as you typed it.
The syntax
from datetime import date
import calendarThese two lines show the two shapes of an import:
import calendarbrings in the whole module. You reach anything inside it through the module's name and a dot:calendar.monthrange(2026, 11).from datetime import datebrings in one name from inside the module. You use that name on its own:date.today().- The module's name must be spelled exactly. Python looks it up letter by letter.
Imports go at the very top of the file, above your functions. Python runs top to bottom, so a name must be imported before any line uses it. With every import first, every line below can use them. Anyone reading your file also sees at once what it borrows.
A minimal working example
from datetime import date
import calendar
today = date.today()
month = today.strftime("%B %Y")
days_in_month = calendar.monthrange(today.year, today.month)[1]
days_left = days_in_month - today.day
print(month)
print(f"Days left this month: {days_left}")Output: today's month and year, then the number of days left in this month. monthrange gets today.year and today.month, so it always checks the current month. Today itself is not counted. On the last day of a month, the second line says Days left this month: 0.
Common mistakes
You use a name before importing it. Run this:
NameError: name 'date' is not definedThe import is there, but it sits below the line that needs it. Python runs top to bottom, so when it ran line 1, date did not exist yet. Leaving the import out completely gives the same error. Move every import to the very top of the file.
You import the module, then use a name from inside it. Run this:
NameError: name 'date' is not definedThis one is confusing, because you did import something. import datetime gives you one name, datetime, and the date inside it stays inside. Either reach in with a dot, datetime.date.today(), or import the name itself with from datetime import date. Your tracker uses the second, so date.today() works on its own.
You misspell the module's name.
ModuleNotFoundError: No module named 'calender'Python looked for a module called calender and found none. The name in quotes is exactly what you typed. Compare it with the real name, letter by letter: calendar ends in ar, not er.
Try it yourself
You will replace the month you typed with today's, and add how many days are left in it.
-
Run the starting code below. Note the line under your title: it is the month you typed into
month. -
At the very top of the file, above
def print_line, add:from datetime import date import calendarThat is the two imports, then two blank lines. Run it. Confirm nothing in your report changed: an import only makes names available.
-
Find the line that starts
month =. Replace that line with these two:today = date.today() month = today.strftime("%B %Y")Run it. Confirm the line under your title shows today's month and year. If you typed this month before, the line looks the same, but Python now writes it.
-
Under your new
monthline, add:days_in_month = calendar.monthrange(today.year, today.month)[1] days_left = days_in_month - today.dayThen, under
print(month), addprint(f"Days left this month: {days_left}"). Run it. Confirm a new line under the month shows the number of days left in this month, not counting today. -
Break it on purpose: change
import calendartoimport calender. Run it. Confirm the console showsModuleNotFoundError: No module named 'calender', and none of your report prints. The import is on line 2, so Python stopped before anyprint. -
Fix it: change
calenderback tocalendar. Run it. Confirm your report returns, with today's month and the days left under your title.
Your tracker now keeps up with the calendar, using code you did not have to write. That completes Part 3. The checkpoint next shows everything your tracker can now do, and what comes after it.