Articles

Calendar program in python to display the calendar of a month in a year

Calendar program in python to display the calendar of a month in a year


In this python program guide, we will learn to write:

  • calendar program in python
  • python program to print calendar of any year
  • python program to check leap year using function

This program will help you to print the calendar of a particular month of a year inputted by the user.

 

In this program, our task is to display the calendar of a month using python. So let’s start.

 

We will be using the Calendar module in python to print a calendar of a month. This Module works similar to the Unix Cal command.

To use this we have to import the calendar module in our respective program.

Please check our video tutorial on calendar program in python:

 

 

🧐🧐Calendar Program in python:

Code to calendar program in python is given below:

import calendar                   # import calendar module in our program
year= int(input("Enter year:"))   # to take input of year from user
month= int(input("Enter month of the year:"))
print(calendar.month(year,month)) # to use the function of calendar module

this program will display the calendar of the particular month of the year inputted by the user.

 

🤷‍♀️🤷‍♀️🤷‍♀️Output to the above program in python is given below:

Enter year:2021
Enter month of the year:5
      May 2021
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

Here you can see the program had been displayed the calendar of a month of a year inputted by user.

 

🧐🧐Options available with the Calendar Module:

1) using function calendar of Calendar module: We can use this function to display the calendar of a whole year.

 

syntax of this function is:

Calendar(year,w,l,c) 

 

📕📕code to see the working of above function in python program to print calendar of any year:
 

import calendar # import calendar module in our program
print("calendar of the year 2021 is: ")
print(calendar.calendar(2021,2,1,6))   # to use the function of calendar module

📙Output :

calendar of the year 2021 is: 
                                  2021

      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       1  2  3  4  5  6  7       1  2  3  4  5  6  7
 4  5  6  7  8  9 10       8  9 10 11 12 13 14       8  9 10 11 12 13 14
11 12 13 14 15 16 17      15 16 17 18 19 20 21      15 16 17 18 19 20 21
18 19 20 21 22 23 24      22 23 24 25 26 27 28      22 23 24 25 26 27 28
25 26 27 28 29 30 31                                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  2  3  4                      1  2          1  2  3  4  5  6
 5  6  7  8  9 10 11       3  4  5  6  7  8  9       7  8  9 10 11 12 13
12 13 14 15 16 17 18      10 11 12 13 14 15 16      14 15 16 17 18 19 20
19 20 21 22 23 24 25      17 18 19 20 21 22 23      21 22 23 24 25 26 27
26 27 28 29 30            24 25 26 27 28 29 30      28 29 30
                          31

        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  2  3  4                         1             1  2  3  4  5
 5  6  7  8  9 10 11       2  3  4  5  6  7  8       6  7  8  9 10 11 12
12 13 14 15 16 17 18       9 10 11 12 13 14 15      13 14 15 16 17 18 19
19 20 21 22 23 24 25      16 17 18 19 20 21 22      20 21 22 23 24 25 26
26 27 28 29 30 31         23 24 25 26 27 28 29      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       1  2  3  4  5  6  7             1  2  3  4  5
 4  5  6  7  8  9 10       8  9 10 11 12 13 14       6  7  8  9 10 11 12
11 12 13 14 15 16 17      15 16 17 18 19 20 21      13 14 15 16 17 18 19
18 19 20 21 22 23 24      22 23 24 25 26 27 28      20 21 22 23 24 25 26
25 26 27 28 29 30 31      29 30                     27 28 29 30 31

The above code shows python program to print calendar of any year.

 

2) isleap(year): this function is used to check whether the year passed to the function is a leap year or not.

 

🔓🔓Lets see python program to check leap year using function. Code to the above function is given below:

import calendar
year=int(input("enter a year"))
print(calendar.isleap(year))

🤷‍♂️🤷‍♂️🤷‍♂️ output :

 

enter a year 2013
False

enter a year 2012
True

So our article on the calendar module in python has come to an end. Hope you like this article - calendar program in python. Share it if you find this article helpful.👍👍👍👍


Basic Python Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Ayush Bajpai
I am a data science enthusiast. I love to learn new technology 🛠🛠.you can reach me at this url www.linkedin.com/in/ayushbajpai47
Page Views :    Published Date : Nov 21,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!