Program to Display Calendar

Program to Display Calendar in Python

To work with date-related tasks, Python has a built-in function called calendar. In this posts, you will learn how to display the calendar for a specific date.

The calendar class in Python’s Calendar module allows for calculations based on date, month, and year for a variety of tasks. Furthermore, the Text Calendar and HTML Calendar classes in Python allow you to customize the calendar and use it as needed.

Examples:

Input:

given year=2001 month =2

Output:

   February 2001
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

Program to Display Calendar in Python

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

1)Displaying the month

To print a calendar in Python, we can use the built-in function month() from the Calendar module. To display a Calendar for a given month, the function month() requires two arguments: the first is the year in four-digit format, such as 2003, 1997, 2018, and the second is the month in two-digit format, such as 01, 04, 12, and so on.

This program prompts the user for the year and month, and then calls the month() function, which displays the Calendar for the given month for a given year, based on the input.

Below is the implementation:

# importing calendar function
import calendar

# given year
given_year = 2001

# given month
given_month = 2

# printing the calendar of given year and month
print(calendar.month(given_year, given_month))

Output:

   February 2001
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

2)Displaying the year

The calendar module is imported in the program  below. The module’s built-in function calender() accepts a year and displays the calendar for that year.

Below is the implementation:

# importing calendar function
import calendar
# given year
given_year = 2001
# printing the calendar of given year
print(calendar.calendar(given_year))

Output:

                                                            2001
   
      January                   February                   March
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
        1  2  3  4  5  6  7                1  2  3  4                1  2  3  4
 8    9  10  11 12 13 14       5  6  7  8  9 10 11       5  6  7  8  9 10 11
15  16  17   18   19   20   21      12 13 14 15 16 17 18      12 13 14 15 16 17 18
22  23  24  25 26 27 28      19 20 21 22 23 24 25      19 20 21 22 23 24 25
29 30 31                  26 27 28                  26 27 28 29 30 31

       April                      May                       June
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
                   1                         1  2  3  4  5  6                   1  2  3
 2  3  4  5  6  7  8                7  8  9 10 11 12 13          4  5  6  7  8  9 10
 9 10 11 12 13 14 15      14 15 16 17 18 19 20        11 12 13 14 15 16 17
16 17 18 19 20 21 22      21 22 23 24 25 26 27        18 19 20 21 22 23 24
23 24 25 26 27 28 29      28 29 30 31                            25 26 27 28 29 30
30

        July                                August                   September
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
                   1                          1  2  3  4  5                                              1  2
 2  3  4  5  6  7  8                  6  7  8  9 10 11 12                   3  4  5  6  7  8  9
 9 10 11 12 13 14 15      13 14 15 16 17 18 19         10 11 12 13 14 15 16
16 17 18 19 20 21 22      20 21 22 23 24 25 26         17 18 19 20 21 22 23
23 24 25 26 27 28 29      27 28 29 30 31                 24 25 26 27 28 29 30
30 31

      October                   November                  December
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7                1  2  3  4                            1  2
 8  9 10 11 12 13 14                5  6  7  8  9 10 11                      3  4  5  6  7  8  9
15 16 17 18 19 20 21      12 13 14 15 16 17 18      10 11 12 13 14 15 16
22 23 24 25 26 27 28      19 20 21 22 23 24 25      17 18 19 20 21 22 23
29 30 31                             26 27 28 29 30            24 25 26 27 28 29 30
                                                    31

 
Related Programs: