Calendar Functions in Python | Set 1 (calendar (), month (), isleap () …)



Calendar operations:

1. calendar (year, W, L, C): — This function displays year, character width, no. rows per week and column splitting.

2. firstweekday () : — this function returns the number of the day of the first week . The default is 0 (Monday).

# Python code to demonstrate how it works
# calendar () and firstweeksday ()

 
# import of the calendar module for calendar operations

import calendar

 
# use the calendar to print the calendar of the year
# print the 2012 calendar

print ( "The calender of year 2012 is:" )

print (calendar.calendar ( 2012 ,   2 , 1 , 6 ))

 
# using firstweekday () to print the starting day number

print ( " The starting day number in calendar is: " , end = " ")

print (calendar.firstweekday ())

Exit:

 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 specified in the argument is leap or not .

4. leapdays (year1, year2) : — This function returns the number of leap days between the given years in arguments.

# Python code to demonstrate how it works
# isleap () and leapdays ()

 
# import of the calendar module for calendar operations

import calendar

 
# using isleap () to check if a year is a leap year

if (calendar.isleap ( 2008 )):

print ( "The year is leap " )

else : print ( "The year is not leap" )

 
# using leapdays () to print leap days between years

print ( " 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 the specified year given in the arguments.  Requires 4 arguments, year, month, character width and no. rows occupied by the week .

# Python code to demonstrate how it works
# month ()

 
# import of the calendar module for calendar operations

import calendar

  
# using month () to display the month of a specific year

print ( "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 

This article is provided by Manjeet Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.