{"id":6158,"date":"2022-06-10T18:40:54","date_gmt":"2022-06-10T16:40:54","guid":{"rendered":"http:\/\/miro.borodziuk.eu\/?p=6158"},"modified":"2026-01-15T10:05:31","modified_gmt":"2026-01-15T09:05:31","slug":"the-calendar-module-in-python","status":"publish","type":"post","link":"http:\/\/miro.borodziuk.eu\/index.php\/2022\/06\/10\/the-calendar-module-in-python\/","title":{"rendered":"The calendar module in Python"},"content":{"rendered":"<p>In addition to the <code>datetime<\/code> and <code>time<\/code> modules, the Python standard library provides a module called <code>calendar<\/code> which, as the name suggests, offers <b>calendar-related functions<\/b>.\u00a0One of them is of course displaying the calendar.<\/p>\n<p><!--more--><\/p>\n<p>1. In the <strong><code>calendar<\/code> <\/strong>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.<\/p>\n<p>&nbsp;<\/p>\n<p>2. To display a calendar for any year, call the <strong><code>calendar()<\/code> <\/strong>function with the year passed as its argument, e.g.:<\/p>\n<pre class=\"lang:default decode:true \">import calendar\r\nprint(calendar.calendar(2020))<\/pre>\n<p>Note: A good alternative to the above function is the function called <strong><code>prcal()<\/code><\/strong>, which also takes the same parameters as the <strong><code>calendar()<\/code> <\/strong>function, but doesn&#8217;t require the use of the <code>print<\/code> function to display the calendar.<\/p>\n<pre class=\"lang:default decode:true \">import calendar\r\ncalendar.prcal(2020)<\/pre>\n<p>&nbsp;<\/p>\n<p>3. To display a calendar for any month of the year, call the <code><strong>month()<\/strong><\/code> function, passing year and month to it.\u00a0 For example:<\/p>\n<pre class=\"lang:default decode:true \">import calendar\r\nprint(calendar.month(2020, 9))\r\n\r\n<\/pre>\n<p>Note: You can also use the <code><strong>prmonth()<\/strong><\/code> function, which has the same parameters as the <strong><code>month()<\/code> <\/strong>function, but doesn&#8217;t require the use of the <code>print<\/code> function to display the calendar.<\/p>\n<p>&nbsp;<\/p>\n<p>4. The <strong><code>setfirstweekday()<\/code> <\/strong>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.<\/p>\n<pre class=\"lang:default decode:true \">import calendar\r\n\r\ncalendar.setfirstweekday(calendar.SUNDAY)\r\ncalendar.prmonth(2020, 12)<\/pre>\n<p>&nbsp;<\/p>\n<p>5. The result of the <strong><code>weekday()<\/code> <\/strong>function is a day of the week as an integer value for a given year, month, and day:<\/p>\n<pre class=\"lang:default decode:true\">import calendar\r\nprint(calendar.weekday(2020, 9, 29)) \r\n# This displays 1, which means Tuesday.<\/pre>\n<p>&nbsp;<\/p>\n<p>6. The <strong><code>weekheader()<\/code> <\/strong>function returns the weekday names in a shortened form. The <strong><code>weekheader()<\/code> <\/strong>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&#8217;ll still get the abbreviated weekday names consisting of only three characters. For example:<\/p>\n<pre class=\"lang:default decode:true\">import calendar\r\nprint(calendar.weekheader(2)) \r\n# This display: Mo Tu We Th Fr Sa Su\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>7.\u00a0The <code>calendar<\/code> module provides two useful functions to check whether years are leap years. The first one, called <strong><code>isleap()<\/code><\/strong>, returns <i>True<\/i> if the passed year is leap, or <i>False<\/i> otherwise. The second one, called <strong><code>leapdays()<\/code><\/strong>, returns the number of leap years in the given range of years.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6181\" src=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/isleap.jpg\" alt=\"\" width=\"432\" height=\"349\" srcset=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/isleap.jpg 432w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/isleap-300x242.jpg 300w\" sizes=\"(max-width: 432px) 100vw, 432px\" \/><\/p>\n<pre class=\"lang:default decode:true\">import calendar\r\n\r\nprint(calendar.isleap(2020))\r\nprint(calendar.leapdays(2010, 2021))  # Up to but not including 2021.\r\n<\/pre>\n<p><code>True<\/code><br \/>\n<code>3<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>8. Classes for creating calendars:<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li><code>calendar.Calendar<\/code> \u2013 provides methods to prepare calendar data for formatting;<\/li>\n<li><code>calendar.TextCalendar<\/code> \u2013 is used to create regular text calendars;<\/li>\n<li><code>calendar.HTMLCalendar<\/code> \u2013 is used to create HTML calendars;<\/li>\n<li><code>calendar.LocalTextCalendar<\/code> \u2013 is a subclass of the <code>calendar.TextCalendar<\/code> class. The constructor of this class takes the <i>locale<\/i> parameter, which is used to return the appropriate months and weekday names.<\/li>\n<li><code>calendar.LocalHTMLCalendar<\/code> \u2013 is a subclass of the <code>calendar.HTMLCalendar<\/code> class. The constructor of this class takes the locale parameter, which is used to return the appropriate months and weekday names.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>9. You can create a <code>calendar<\/code> object yourself using the <code>Calendar<\/code> class, which, when creating its object, allows you to change the first day of the week with the optional <code>firstweekday<\/code> parameter, e.g.:<\/p>\n<pre class=\"lang:default decode:true\">import calendar  \r\n\r\nc = calendar.Calendar(2)\r\n\r\nfor weekday in c.iterweekdays():\r\n    print(weekday, end=\" \")\r\n# Result: 2 3 4 5 6 0 1\r\n<\/pre>\n<p>The <code><strong>iterweekdays()<\/strong><\/code> returns an iterator for weekday numbers. The first value returned is always equal to the value of the <code>firstweekday<\/code> property.<\/p>\n<p>&nbsp;<\/p>\n<p>10. Another example of methods that return an iterator is the <strong><code>itermonthdates()<\/code><\/strong> method, which requires specifying the year and month.<\/p>\n<p>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.<\/p>\n<p>Each day is represented by a <code>datetime.date<\/code> object.<\/p>\n<pre class=\"lang:default decode:true\">import calendar  \r\n\r\nc = calendar.Calendar()\r\n\r\nfor date in c.itermonthdates(2019, 11):\r\n    print(date, end=\" \")<\/pre>\n<p><code>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<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>11. Another useful method in the <code>Calendar<\/code> class is the method called <strong><code>itermonthdays()<\/code><\/strong>, which takes year and month as parameters, and then returns the iterator to the days of the week represented by numbers.<\/p>\n<pre class=\"lang:default decode:true \">import calendar  \r\n\r\nc = calendar.Calendar()\r\n\r\nfor iter in c.itermonthdays(2019, 11):\r\n    print(iter, end=\" \")<\/pre>\n<p><code>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<\/code><\/p>\n<p>There are four other similar methods in the <code>Calendar<\/code> class that differ in data returned:<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li><strong><code>itermonthdays2<\/code> <\/strong>\u2013 returns days in the form of tuples consisting of a day of the month number and a week day number;<\/li>\n<li><strong><code>itermonthdays3<\/code> <\/strong>\u2013 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;<\/li>\n<li><strong><code>itermonthdays4<\/code> <\/strong>\u2013 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.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>12. The <code>Calendar<\/code> class has several other useful methods that you can learn more about in the documentation (<a href=\"https:\/\/docs.python.org\/3\/library\/calendar.html\" target=\"_blank\" rel=\"noopener\">https:\/\/docs.python.org\/3\/library\/calendar.html<\/a>).<\/p>\n<p>One of them is the <strong><code>monthdays2calendar()<\/code> <\/strong>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.<\/p>\n<pre class=\"lang:default decode:true \">import calendar  \r\n\r\nc = calendar.Calendar()\r\n\r\nfor data in c.monthdays2calendar(2020, 12):\r\n    print(data)<\/pre>\n<p><code>[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]<\/code><br \/>\n<code>[(7, 0), (8, 1), (9, 2), (10, 3), (11, 4), (12, 5), (13, 6)]<\/code><br \/>\n<code>[(14, 0), (15, 1), (16, 2), (17, 3), (18, 4), (19, 5), (20, 6)]<\/code><br \/>\n<code>[(21, 0), (22, 1), (23, 2), (24, 3), (25, 4), (26, 5), (27, 6)]<\/code><br \/>\n<code>[(28, 0), (29, 1), (30, 2), (31, 3), (0, 4), (0, 5), (0, 6)]<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 1<\/strong><\/p>\n<p>What is the output of the following snippet?<\/p>\n<pre class=\"lang:default decode:true \">import calendar\r\nprint(calendar.weekheader(1))<\/pre>\n<p>&nbsp;<\/p>\n<p id=\"sol\"><code class=\"codep \">M T W T F S S<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 2<\/strong><\/p>\n<p>What is the output of the following snippet?<\/p>\n<pre class=\"lang:default decode:true \">import calendar  \r\n\r\nc = calendar.Calendar()\r\n\r\nfor weekday in c.iterweekdays():\r\n    print(weekday, end=\" \")<\/pre>\n<p>&nbsp;<\/p>\n<p><code class=\"codep \">0 1 2 3 4 5 6<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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.\u00a0One of them is of course displaying the calendar.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[],"_links":{"self":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/6158"}],"collection":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/comments?post=6158"}],"version-history":[{"count":16,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/6158\/revisions"}],"predecessor-version":[{"id":6176,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/6158\/revisions\/6176"}],"wp:attachment":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media?parent=6158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/categories?post=6158"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/tags?post=6158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}