Python offers a built-in calendar module that lets you manipulate code for specific days or months of the year. You could use it to output a string of all the calendar months in a year, for example.

Here's how to get started with Python's calendar class and make a DIY calendar with it.

How to Get Started With the Calendar Module

The calendar module is easy to use. And since it's a built-in Python module, you don't need to install it separately.

To get started, open your Python file and import the calendar module:

        import calendar

See the Days of the Week

Starting with Monday, the module's default starting day of the week, you can output the days of the week in an abbreviated form using the weekheader() function.

The weekheader() function of the module, however, accepts a width argument. This is an integer that specifies the number or length of the abbreviated string:

        import calendar
print(calendar.weekheader(2)) <strong># set the abbreviation length to 2</strong>

<strong>Output: Mo Tu We Th Fr Sa Su
</strong>

The module sets the first day of the week to Monday by default. You can change this to Sunday using the setfirstweekday() function of the calendar module.

Now try to set the first day of the week back to Sunday. Then reprint the week headers as you did above:

        import calendar
<strong># set the first day of the week to Sunday: </strong>
calendar.setfirstweekday(calendar.SUNDAY)

print(calendar.weekheader(3)) <strong># set the abbreviation length to 3 </strong>
<strong>Output: Sun Mon Tue Wed Thu Fri Sat
</strong>

See if a Year Is a Leap or Not

Python lets you check whether a year is a leap or not when you invoke the isleap() function from the calendar class.

The function, however, accepts a year argument (2023 as in the below code) and returns a Boolean output:

        import calendar
print(calendar.isleap(2023))

<strong>Output: False </strong>

Check the Number of Leap Days Between Specific Year Ranges

You use the leapdays() to check the number of leap days between specific year ranges.

For instance, to check the number of leap days between 2021 and 2030:

        import calendar
checkleap = calendar.leapdays(2022, 2030)

print("There are {} leap days between 2022 and 2030".format(checkleap))

<strong>Output: There are 2 leap days between 2022 and 2030
</strong>

Related: How Does the Python String format() Method Work?

Check the First Week Day in a Month

The module returns a list index of the days in a week. In essence, Monday, the default first weekday, is equivalent to zero, while Tuesday is one—in that order, up to Sunday, the last weekday, which is the sixth index.

You can confirm this by printing the default first day of the week using the firstweekday() method of the calendar class:

        import calendar
print(calendar.firstweekday())

<strong>Output: 0</strong>

Using this insight, let's check the first day of September 2020 using the weekday() function:

        import calender
print(calendar.weekday(2021, 9, 1)) <strong># This accepts a year, a month, and a date argument</strong>

<strong>Output: 2</strong>

Following the default index, 2, here means the first day of September 2020 is a Wednesday.

Related:How Arrays and Lists Work in Python

You can also check the first day of the month and see the number of days in that month.

To do that, use monthrange():

        import calendar
print(calendar.monthrange(2021, 9))
<strong>Output: (2, 30)</strong>

Output a Month Calendar

To see the calendar for a month, use the month() function from the calendar class. In addition to the year and month of interest, it accepts optional width (w) and length (l) arguments.

These arguments are optional. So the module sets them to zero by default even if you don't specify them.

Here's how to print the monthly calendar using the month() method of this class:

        import calendar
print(calendar.month(theyear = 2021, themonth = 9, w = 4, l = 2))

That looks like this:

Month calendar output

Output a Calendar Year

Ultimately, you can output the entire calendar year using the calendar function of the module:

        import calendar
print(calendar.calendar(2022, w=2, l=1, c=2, m=6))

Here's what the output looks like:

Yearly calendar output

While w controls the width and l controls the length between each string, c and m specify the number of rows and columns, respectively. Feel free to tweak the values of these parameters to see what happens.

That's it! You just created a calendar using Python. If you're curious, you can also look at the calendar module documentation to learn more about its various methods and features.

Practice More With Python Calendar Module

We've only discussed the basic concepts of the calendar module in this article. There's more to it than what this article could cover. The module, for instance, offers HTML and CSS decorators and widgets for styling your calendar and presenting it as HTML. Plus, it has comprehensive documentation. So feel free to play around with it as you like. Happy coding!