Calendar module in Python

In this tutorial, I am going to tell you about the calendar module in Python. It is a built-in module of python. Python provides a calendar module to display the calendar. To use its various functions, you have import this module.

import calendar

Calendar module in Python with major functions

This module has various functions which will tell us about the leap year, weekdays in a month and display the calendar of the month and the year.

  1. weekday(year, month, day)
    This function returns the weekday (1-6 ~ Monday- Saturday) for the year, month (1-12) and day (1-31).
    Ex: weekday (2019,6,19)
    returns 2 because this function will count the weekdays (from Monday to Saturday) for the given year, month and day. The day is 19 which is a Wednesday in a month of June (6). So, it will count Monday and Tuesday as the weekdays and return the         2 as the answer.

    # import the module 
    import calendar
    
    # use of weekday()
    print("Weekday:",calendar.weekday(2019,6,19))
    

    Output:-

    Weekday: 2
  2. prmonth (year, month)
    This function displays the month’s calendar.
    Ex: prmonth(2019, 6)
    returns the calendar of the June 2019.

    # import the module
    import calendar
    
    # use of prmonth()
    print("Calendar of June 2019:",calendar.prmonth(2019,6))
    

    Output:-

     June 2019
    
    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
    Calendar of June 2019: None
  3. monthrange (year, month)
    This function returns the weekday (0-6 ~ Monday- Sunday) and number of days (28-31) for year and month.
    Ex: monthrange(2019, 6)
    returns (5, 30) as 5 is the weekday (i.e., as the June starts from Saturday. So, it will count from Monday to Friday) and 30 is the number of days in the June month.

    import calendar
    
    # use of monthrange()
    print("monthrange of june:",calendar.monthrange(2019,6))
    

    Output:-

    monthrange of june: (5, 30)
  4. isleap (year)
    This function returns true for leap year, false for non-leap year.
    Ex: isleap (2016)
    returns true as 2016 is a leap year. If the given year is not a leap year, then it returns false.

    import calendar
    
    # use of isleap()
    print("2019 is leap year:",calendar.isleap(2019))
    print("2016 is leap year:",calendar.isleap(2016))
    

    Output:-

    2019 is leap year: False
    2016 is leap year: True
  5. leapdays (year 1, year 2)
    This function returns number of leap year in a range [ year 1, year 2). Assume that year 1<= year 2.
    Ex: leapdays ( 2014, 2019)
    returns 1 as 2016 is the leap year in between the range from 2014 to 2019.

    import calendar
    
    # use of leapdays()
    print("Number of leapyears:",calendar.leapdays(2014,2019))
    

    Output:-

    Number of leapyears: 1
  6. prcal (year)
    This function returns year’s calendar.
    Ex: prcal (2019)
    returns the 2019 calendar.

    import calendar
    
    # use of  prcal()
    print("2019 year calendar:",calendar.prcal(2019))
    

    Output:-

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

Also read:

Leave a Reply

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