In addition to the datetime and time modules, the Python standard library provides a module called calendar which, as the name suggests, offers calendar-related functions. One of them is of course displaying the calendar.
1. In the calendar module, the days of the week are displayed from Monday to Sunday. Each day of the week has its representation in the form of an integer, where the first day of the week (Monday) is represented by the value 0, while the last day of the week (Sunday) is represented by the value 6.
2. To display a calendar for any year, call the calendar() function with the year passed as its argument, e.g.:
|
1 2 |
import calendar print(calendar.calendar(2020)) |
Note: A good alternative to the above function is the function called prcal(), which also takes the same parameters as the calendar() function, but doesn’t require the use of the print function to display the calendar.
|
1 2 |
import calendar calendar.prcal(2020) |
3. To display a calendar for any month of the year, call the month() function, passing year and month to it. For example:
|
1 2 |
import calendar print(calendar.month(2020, 9)) |
Note: You can also use the prmonth() function, which has the same parameters as the month() function, but doesn’t require the use of the print function to display the calendar.
4. The setfirstweekday() function allows you to change the first day of the week. It takes a value from 0 to 6, where 0 is Sunday and 6 is Saturday.
|
1 2 3 4 |
import calendar calendar.setfirstweekday(calendar.SUNDAY) calendar.prmonth(2020, 12) |
5. The result of the weekday() function is a day of the week as an integer value for a given year, month, and day:
|
1 2 3 |
import calendar print(calendar.weekday(2020, 9, 29)) # This displays 1, which means Tuesday. |
6. The weekheader() function returns the weekday names in a shortened form. The weekheader() method requires you to specify the width in characters for one day of the week. If the width you provide is greater than 3, you’ll still get the abbreviated weekday names consisting of only three characters. For example:
|
1 2 3 |
import calendar print(calendar.weekheader(2)) # This display: Mo Tu We Th Fr Sa Su |
7. The calendar module provides two useful functions to check whether years are leap years. The first one, called isleap(), returns True if the passed year is leap, or False otherwise. The second one, called leapdays(), returns the number of leap years in the given range of years.

|
1 2 3 4 |
import calendar print(calendar.isleap(2020)) print(calendar.leapdays(2010, 2021)) # Up to but not including 2021. |
True
3
8. Classes for creating calendars:
-
calendar.Calendar– provides methods to prepare calendar data for formatting;calendar.TextCalendar– is used to create regular text calendars;calendar.HTMLCalendar– is used to create HTML calendars;calendar.LocalTextCalendar– is a subclass of thecalendar.TextCalendarclass. The constructor of this class takes the locale parameter, which is used to return the appropriate months and weekday names.calendar.LocalHTMLCalendar– is a subclass of thecalendar.HTMLCalendarclass. The constructor of this class takes the locale parameter, which is used to return the appropriate months and weekday names.
9. You can create a calendar object yourself using the Calendar class, which, when creating its object, allows you to change the first day of the week with the optional firstweekday parameter, e.g.:
|
1 2 3 4 5 6 7 |
import calendar c = calendar.Calendar(2) for weekday in c.iterweekdays(): print(weekday, end=" ") # Result: 2 3 4 5 6 0 1 |
The iterweekdays() returns an iterator for weekday numbers. The first value returned is always equal to the value of the firstweekday property.
10. Another example of methods that return an iterator is the itermonthdates() method, which requires specifying the year and month.
As a result, all days in the specified month and year are returned, as well as all days before the beginning of the month or the end of the month that are necessary to get a complete week.
Each day is represented by a datetime.date object.
|
1 2 3 4 5 6 |
import calendar c = calendar.Calendar() for date in c.itermonthdates(2019, 11): print(date, end=" ") |
2019-10-28 2019-10-29 2019-10-30 2019-10-31 2019-11-01 2019-11-02 2019-11-03 .... 2019-11-29 2019-11-30 2019-12-01
11. Another useful method in the Calendar class is the method called itermonthdays(), which takes year and month as parameters, and then returns the iterator to the days of the week represented by numbers.
|
1 2 3 4 5 6 |
import calendar c = calendar.Calendar() for iter in c.itermonthdays(2019, 11): print(iter, end=" ") |
0 0 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 0
There are four other similar methods in the Calendar class that differ in data returned:
-
itermonthdays2– returns days in the form of tuples consisting of a day of the month number and a week day number;itermonthdays3– returns days in the form of tuples consisting of a year, a month, and a day of the month numbers. This method has been available since version 3.7;itermonthdays4– returns days in the form of tuples consisting of a year, a month, a day of the month, and a day of the week numbers. This method has been available since Python version 3.7.
12. The Calendar class has several other useful methods that you can learn more about in the documentation (https://docs.python.org/3/library/calendar.html).
One of them is the monthdays2calendar() method, which takes the year and month, and then returns a list of weeks in a specific month. Each week is a tuple consisting of day numbers and weekday numbers. Look at the code in the editor.
|
1 2 3 4 5 6 |
import calendar c = calendar.Calendar() for data in c.monthdays2calendar(2020, 12): print(data) |
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]
[(7, 0), (8, 1), (9, 2), (10, 3), (11, 4), (12, 5), (13, 6)]
[(14, 0), (15, 1), (16, 2), (17, 3), (18, 4), (19, 5), (20, 6)]
[(21, 0), (22, 1), (23, 2), (24, 3), (25, 4), (26, 5), (27, 6)]
[(28, 0), (29, 1), (30, 2), (31, 3), (0, 4), (0, 5), (0, 6)]
Exercise 1
What is the output of the following snippet?
|
1 2 |
import calendar print(calendar.weekheader(1)) |
M T W T F S S
Exercise 2
What is the output of the following snippet?
|
1 2 3 4 5 6 |
import calendar c = calendar.Calendar() for weekday in c.iterweekdays(): print(weekday, end=" ") |
0 1 2 3 4 5 6

