AI Skill Report Card

Working with Calendar Module

A-85·Aug 15, 2026·Source: Web

Working with Python's calendar Module

14 / 15
Python
import calendar # Print a text calendar for a month print(calendar.month(2024, 3)) # Print a full year calendar print(calendar.calendar(2024)) # Check leap year calendar.isleap(2024) # True # Get weekday of a specific date (0=Monday) calendar.weekday(2024, 3, 15) # Friday -> 4 # Get (first_weekday, number_of_days) for a month calendar.monthrange(2024, 2) # (3, 29) -> Feb 2024 starts Thu, has 29 days
Recommendation
Add an example showing a bad/common mistake output (e.g., forgetting to filter padding zeros) to contrast good vs bad outcomes explicitly
13 / 15

Progress:

  • Step 1: Decide output format needed (text, HTML, or raw data structures)
  • Step 2: Choose the right class/function (Calendar, TextCalendar, HTMLCalendar, or module-level convenience functions)
  • Step 3: Set locale/first-weekday if non-default behavior required
  • Step 4: Generate data (iterators for weeks/months/years, or formatted strings)
  • Step 5: Post-process output (e.g., strip empty days marked 0, style HTML)

Choosing the right tool

  • Quick print/debugcalendar.month(year, month), calendar.calendar(year)
  • Iterate raw day numbers (0 = day outside month) → Calendar.itermonthdays()
  • Iterate (day, weekday) tuplesCalendar.itermonthdays2()
  • Iterate actual datetime.date objectsCalendar.itermonthdates() (note: can spill into adjacent years at Dec/Jan boundaries)
  • Group days into weeksCalendar.monthdayscalendar() / monthdatescalendar()
  • HTML outputHTMLCalendar class, formatmonth()/formatyear()
  • Locale-specific namesLocaleTextCalendar / LocaleHTMLCalendar

Setting the first day of the week

Python
c = calendar.Calendar(firstweekday=6) # Sunday-first calendar.setfirstweekday(calendar.SUNDAY) # affects module-level functions globally

Default is Monday (0). Constants: MONDAY=0 ... SUNDAY=6.

Recommendation
Include a brief example of LocaleTextCalendar usage since it's mentioned but never demonstrated
17 / 20

Example 1: Get weeks of a month as date objects

Input:

Python
import calendar cal = calendar.Calendar() list(cal.monthdatescalendar(2024, 2))

Output:

Python
[[datetime.date(2024, 1, 29), ..., datetime.date(2024, 2, 4)], [datetime.date(2024, 2, 5), ..., datetime.date(2024, 2, 11)], ... [datetime.date(2024, 2, 26), ..., datetime.date(2024, 3, 3)]]

Each inner list has exactly 7 dates; padding days come from adjacent months.

Example 2: Generate an HTML calendar for a month

Input:

Python
import calendar hc = calendar.HTMLCalendar(firstweekday=calendar.SUNDAY) html = hc.formatmonth(2024, 12)

Output: A string of <table class="month">...</table> HTML with <td class="mon">, <td class="noday"> for padding cells, etc. — ready to embed in a page or style with CSS classes (month, year, noday, mon, tue, ... , sun).

Example 3: Count weekday occurrences in a month

Input:

Python
calendar.monthrange(2024, 2)

Output:

Python
(3, 29) # Feb 2024 starts on a Thursday (index 3), has 29 days

Use this to compute how many Mondays/Fridays/etc. occur in a month without iterating.

Recommendation
Consider trimming the Best Practices and Common Pitfalls sections slightly since there's some overlap/redundancy between them
  • Prefer Calendar iterator methods over string parsing of calendar.month()/calendar.calendar() output when you need structured data — the text output is for display only.
  • Use itermonthdates() cautiously: it can raise OverflowError near datetime.MAXYEAR/MINYEAR boundaries since it includes adjacent-month dates. Use itermonthdays() (plain ints, 0 = padding) if you need to avoid date-range edge cases.
  • Use calendar.isleap(year) and calendar.leapdays(y1, y2) rather than reimplementing leap-year math.
  • For locale-aware month/day names, use LocaleTextCalendar/LocaleHTMLCalendar with a (locale, encoding) tuple, or the module-level calendar.month_name/calendar.day_name arrays (locale-independent, English by default).
  • Remember month_name and day_name are 1-indexed for months (month_name[0] is '') but day_name is 0-indexed starting Monday.
  • For CLI-style output, python -m calendar 2024 works directly from the command line — useful for quick checks without writing a script.
  • Assuming week lists always belong to the queried monthmonthdatescalendar/monthdayscalendar pad with adjacent-month days (represented as 0 in the days variant); filter these out if you only want the target month.
  • Confusing weekday() (needs date) with firstweekday() (calendar setting)calendar.weekday(y, m, d) returns which day of week a specific date falls on; Calendar.getfirstweekday() returns the configured start-of-week for layout purposes.
  • Forgetting setfirstweekday() is global/stateful for module-level functions — prefer instantiating your own Calendar/TextCalendar/HTMLCalendar object with firstweekday set explicitly to avoid side effects across code that shares the module.
  • Mixing up month indicescalendar months are 1–12 (no month 0), unlike some other date libraries.
  • Using calendar.calendar()/month() for programmatic parsing — these produce human-formatted strings with locale-dependent spacing; use the iterator/data methods instead.
0
Grade A-AI Skill Framework
Scorecard
Criteria Breakdown
Quick Start
14/15
Workflow
13/15
Examples
17/20
Completeness
18/20
Format
15/15
Conciseness
14/15