Create a month as rows of seven day numbers.
How It Works
calendar.monthcalendar(year, month) returns a list of week lists. Each week has seven weekday positions, and zeros represent dates outside that month.
Step-by-Step Example
import calendar
weeks = calendar.monthcalendar(2026, 10)
print(all(len(week) == 7 for week in weeks))
print(any(day == 0 for week in weeks for day in week))
Understanding the Example
- The expression 'calendar.monthcalendar(2026, 10)' supplies the value stored as 'weeks'. This value is then used or checked by the later statements.
- The print() call exposes the result so you can compare it with the behavior described above. Check the value and its type when experimenting.
A successful run of this example prints the following output:
True
True
Where This Fits in a Real Program
For event timestamps, distinguish the displayed local clock from the instant that it represents. Store or exchange an unambiguous aware value, then convert it when displaying it to a user. For repeated calendar appointments, use a geographic timezone so daylight-saving rules are not lost.
Practice: Try It Yourself
setfirstweekday() can alter calendar presentation conventions.
For this exercise, predict the result before running the changed code. Then explain the difference between the original and modified cases using the actual values, not just whether the program completed.
Further Reading
Read the official Python documentation for complete parameters, return types and exceptions.
Common Beginner Mistakes
- Treating zero-filled cells as actual dates or forgetting the weekday ordering.
- Display a month as a table and ignore cells containing zero.
Key Takeaways
- Create a month as rows of seven day numbers.
- calendar.monthcalendar(year, month) returns a list of week lists.
- Apply the demonstrated operation to the practice input and verify its result before depending on it in a larger program.