Python Examples

Python Program to Display Calendar

In Python, we can display the calendar by importing the calendar module to our program. The calendar python module’s built-in function month() takes the year and the month as input and shows the calendar for the input entered by the user. 

How do you make a Calendar in Python?

                    

Source Code

import calendar

#initialize the year and the month variables

yr = 2020

mnth = 1

# printing the calendar

print(calendar.month(yr, mnth))

Output:

January 2020

Mo Tu We Th Fr Sa Su

      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 31

What is the Calendar Module in Python?

The calendar python module is an in-built module in Python that handles operations related to calendars. The output of the module is displayed as a calendar. By default, the first day of the week for the Gregorian calendars is Monday, and the last day is Sunday.

calendar python

Source

For example:

If you want to generate the calendar for the year 2021, follow the steps given below:

                    

# importing the module

import calendar

# initializing the year

yr = 2021

# printing the calendar for 2021

print(calendar.calendar(yr))


Output:

After compilation, the calendar for the year 2021 will be generated as the output for the above program

How do I Print a whole year Calendar in Python?

If one wants to print a whole year as output, the user must enter a valid year as the input, and using the calendar module in Python, one can simply print the calendar for that respective year. 

Follow the steps given below:

  • The first step is to import the calendar module 
  • Initialize the year you desire
  • The year is taken via the module’s built-in function calendar().
  • Use the calendar to print the calendar for a specific year. calendar(year)

                    

Source Code:

import calendar //import the calendar module

def printcalendar(yr):


     # printing calendar

     print(calendar.calendar(yr))
  

# driver program

yr = 2019

printcalendar(yr)

Output:

The whole calendar year of 2019 will be displayed as the output.

What is the name of Python’s built-in module for Working with Calendars?

The name of Python’s built-in function is called “Calendar.” This function is used for many calendar-related operations. In addition to this, there are specific modules that will help the users to edit or manipulate the calenders as per their desire. 

The most important classes for manipulation and to display the calendars include: 

Calendar, TextCalendar, and HTMLCalendar

class calendar.Calendar

This class is used for formatting the calendar data. The class allows the data to be formatted based upon the day, date, month, and year. 

class calendar.TextCalendar 

The TextCalendar class in Python, allows you to customize the calendar and utilize it as needed. This class is used to generate plain text calendars. 

class calendar.HTMLCalendar

If a user wants to generate an HTML calendar he/she can use this class. The HTMLCalendar class in Python allows you to customize the calendar and utilize it as needed.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.