SypherTec
…
PART 3 · 08 OF 085 MIN READUPDATED 20 NOV 2026

Modules: Using Code That Comes With Python

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:

WHERE YOU LEFT OFF · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Look at the line under your title. You typed that month by hand. Now run this:

TODAY'S DATE, IN PARTS · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

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:

THE WHOLE CALENDAR MODULE · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

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:

A DATE AS WORDS · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

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:

  • %B is the month's full name, such as November.
  • %Y is the year, with four digits.
  • %d is the day of the month, always two digits, so the 5th shows as 05.

Everything else in the pattern, such as the spaces, stays exactly as you typed it.

The syntax

from datetime import date
import calendar

These two lines show the two shapes of an import:

  • import calendar brings in the whole module. You reach anything inside it through the module's name and a dot: calendar.monthrange(2026, 11).
  • from datetime import date brings 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

main.py
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:

THE IMPORT COMES TOO LATE · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output
NameError: name 'date' is not defined

The 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:

THE MODULE, NOT THE NAME · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output
NameError: name 'date' is not defined

This 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.

  1. Run the starting code below. Note the line under your title: it is the month you typed into month.

  2. At the very top of the file, above def print_line, add:

    from datetime import date
    import calendar
     
     

    That is the two imports, then two blank lines. Run it. Confirm nothing in your report changed: an import only makes names available.

  3. 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.

  4. Under your new month line, add:

    days_in_month = calendar.monthrange(today.year, today.month)[1]
    days_left = days_in_month - today.day

    Then, under print(month), add print(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.

  5. Break it on purpose: change import calendar to import calender. Run it. Confirm the console shows ModuleNotFoundError: No module named 'calender', and none of your report prints. The import is on line 2, so Python stopped before any print.

  6. Fix it: change calender back to calendar. Run it. Confirm your report returns, with today's month and the days left under your title.

TRY IT YOURSELF · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

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.

QUIZ · 3 QUESTIONSCheck what stuckStart the quiz
PART 3 · 08 OF 08❮❯