Calendar Functions in Python | Set 1( calendar(), month(), isleap()...) - GeeksforGeeks

22 Jan, 2022
Python defines an inbuilt module “calendar” which handles operations related to the calendar.Operations on the calendar : 1. calendar(year, w, l, c):- This function displays the year, the width of characters, no. of lines per week, and column separations.2. firstweekday() :- This function returns the first week day number. By default 0 (Monday). 
Python3
# Python code to demonstrate the working of# calendar() and firstweeksday() # importing calendar module for calendar operationsimport calendar # using calendar to print calendar of year# prints calendar of 2012print ("The calendar of year 2012 is : ")print (calendar.calendar(2012,2,1,6)) #using firstweekday() to print starting day numberprint ("The starting day number in calendar is : ",end="")print (calendar.firstweekday())
Output:  
The calendar of year 2012 is : 
                                  2012

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

The starting day number in calendar is : 0
3. isleap (year):- This function checks if the year mentioned in the argument is a leap or not.4. leapdays (year1, year2):- This function returns the number of leap days between the specified years in arguments. 
Python3
# Python code to demonstrate the working of# isleap() and leapdays() # importing calendar module for calendar operationsimport calendar # using isleap() to check if year is leap or notif (calendar.isleap(2008)):       print ("The year is leap")else : print ("The year is not leap") #using leapdays() to print leap days between yearsprint ("The leap days between 1950 and 2000 are : ",end="")print (calendar.leapdays(1950, 2000))
Output:  
The year is leap
The leap days between 1950 and 2000 are : 12
5. month (year, month, w, l) :- This function prints the month of a specific year mentioned in arguments. It takes 4 arguments, year, month, width of characters and no. of lines taken by a week. 
Python3
# Python code to demonstrate the working of# month() # importing calendar module for calendar operationsimport calendar # using month() to display month of specific yearprint ("The month 5th of 2016 is :")print (calendar.month(2016,5,2,1))
Output:  
The month 5th of 2016 is :
      May 2016
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
 
YouTubeGeeksforGeeks507K subscribersPython Programming Tutorial | Calendar Functions - Part 1 | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:34•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=KRz3ZnVz-WY" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
clintra
amartyaghoshgfg
date-time-program
Python Calander-module
Python
School Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects