| by Arround The Web | No comments

Python Calendar Module

Python provides various inbuilt and open source libraries for working with different operations, such as “Pandas” which is used for data analysis, and “OpenCV” which is used for computer vision. The “calendar” module is used for working with time-related tasks in Python. The “calendar” module provides several functions and classes for working with calendars and performing various operations such as generating text or HTML format calendars.

This write-up presents a comprehensive guide on the Python calendar module using numerous examples.

What is the Python “Calendar” Module?

The “calendar” module in Python handles the operations or tasks associated with the calendar. This module provides various functions and methods for performing different operations related to the calendar. The calendar module functions and classes utilize a present Gregorian calendar, an idealized calendar. In these calendars, “Monday” is considered the “1st” weekday and “Sunday” is the last “7th”.

Let’s understand this module using various functions:

Example 1: Using “calendar.month()” Function of Calendar Module

The “calendar.month()” function of the “calendar” module takes the specified year and month as an argument and retrieves the calendar of the specified month. Here is an example.

import calendar
print(calendar.month(2022, 6))

The above code generates the following output:

Example 2: Using “calendar.calendar()” Function

The “calendar.calendar()” function of the “calendar” module can also be used to display the calendar of the specified year. In the below code, this function accepts the year “2019” as an argument and retrieves the calendar of the whole year.

import calendar
print(calendar.calendar(2019))

This code displays the following snippet:

Classes of Calendar Module

The “calendar” module in Python provides three main classes, such as “calendar”, “TextCalendar” and “HTMLCalendar”. The “calendar” and “TextCalendar” classes are utilized to create calendars in text format. The “HTMLCalendar” class is utilized to create/generate HTML format calendars.

Let’s explore these classes one by one using various examples:

Calendar Class

The “Calendar” class creates/generates a calendar object which supports different ways to prepare/ready the calendar data for formatting. The “Calendar” class does not perform formatting itself, as the formatting is done by the subclasses. Let’s understand it via the below code:

Example 1: Using “calendar.iterweekdays()” Method

In the below code, the “calendar.iterweekdays()” method of the “calendar” module is used to retrieve the iterator, which consists of the weekday number list. Here is an example code:

import calendar
cal = calendar.Calendar()
ciw = cal.iterweekdays()
for day in ciw:
    print(day, end=' ')

The above code displays the following output:

Example 2: Using the “calendar.monthdayscalendar()” Method

The “calendar.monthdayscalendar()” method of the “calendar” module is used to retrieve the full week’s list, and every week’s list consists of the month’s days.

import calendar
obj = calendar.Calendar()
print(obj.monthdayscalendar(2022, 9))

The above code shows the following output:

Check this official documentation to get more information related to all the methods of the “calendar” class.

TextCalendar Class

This class in the “calendar” module is utilized to create/generate plain text format calendars. Let’s understand this class by the following examples:

Example 1: Using “TextCalendar.prmonth()” Method

The following code utilizes the “TextCalendar.prmonth()” method of the “TextCalendar” class to print the specified year-month calendar.

import calendar
text_cal = calendar.TextCalendar(firstweekday = 0)
print(text_cal.prmonth(2021, 5))

The above code shows the following output:

Example 2: Using the “TextCalendar.formatmonth()” Method

The “TextCalendar.formatmonth()” method of the “TextCalendar” class is used to show the month calendar in a multiline string. Here is an example:

import calendar
text_cal = calendar.TextCalendar(firstweekday = 0)
print(text_cal.formatmonth(2023, 2))

The above code generates the following output:

Note: The “formatyear()” method and “pryear()” method of the “TextCalendar” class is used to retrieve an m-column calendar and displays the calendar for a complete year.

HTMLCalendar Class

The third main class of the “Calendar” module is the “HTMLCalendar” class which is used to create/generate the calendars in HTML format. Let’s explore this by the following example:

Example 1: Using the “HTMLCalendar.formatmonth()” Method

The “HTMLCalendar.formatmonth()” method of the “HTMLCalendar” class is used to retrieve the HTML table of the specified calendar month. Here is an example code:

import calendar
txt_cl = calendar.HTMLCalendar(firstweekday = 0)
print(txt_cl.formatmonth(2022, 4))

When the above code is executed, the following HTML table displays to the console:

Note: The “formatyear()” and “formatyearpage()” methods of the “HTMLCalendar” class can also be used to retrieve the year’s calendars as an HTML table and HTML page.

Simple Text Calendar

The simple text calendar provides several functions to perform certain operations related to the calendar. Let’s perform various examples using the simple text calendar.

Example 1: Using the “firstweekday()” Method

In the below code, the “firstweekday()” method of the “calendar” module is used to retrieve the first-weekday number, which is “0” by default. The “setfirstweekday()” method takes the “2” as an argument and sets or modifies the first-weekday number.

import calendar
print(calendar.firstweekday())
calendar.setfirstweekday(2)
print(calendar.firstweekday())

The above code displays the following output:

Example 2: Using “calendar.isleap()” Method

Another method named “isleap()” is utilized to verify whether the year is leap or not in Python. In the code below, different years’ values are passed to the function to check whether the year is a leap. This method retrieves “True” when the year is leap and “False” when the year is not leap.

import calendar
print(calendar.isleap(2015))
print(calendar.isleap(2020))
print(calendar.isleap(2023))
print(calendar.isleap(2024))
print(calendar.isleap(2025))
print(calendar.isleap(2028))

The above example code creates the following output:

That was all about Python’s calendar module.

Conclusion

In Python, the “calendar” module provides various methods and classes for dealing with the month, day, and year of the specified calendar. The “calendar” module contains three main classes, for example, “calendar”, “TextCalendar”, and “HTMLCalendar”. These classes provide various methods that can perform different tasks related to calendars. The simple Text Calendar also provides various methods that are used to print specified years, months, and days. This Python write-up delivered a comprehensive tutorial on Python “calendar” module using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply