Python Calendar monthdayscalendar() Method with Example

Python Calendar.monthdayscalendar() Method: In this tutorial, we will learn about the monthdayscalendar() method of Calendar class in Python with its usage, syntax, and examples. By Hritika Rajput Last updated : April 24, 2023

Python Calendar.monthdayscalendar() Method

The Calendar.monthdayscalendar() method is an inbuilt method of the Calendar class of calendar module, it returns a list of the weeks in the given month of the given year as full weeks. Weeks are lists of seven numbers. If the value of a day in a week is 0, it means it does not belong to that month.

Module

The following module is required to use monthdayscalendar() method:

import calendar

Class

The following class is required to use monthdayscalendar() method:

from calendar import Calendar

Syntax

The following is the syntax of monthdayscalendar() method:

monthdayscalendar(year, month)

Parameter(s)

The following are the parameter(s):

  • year: It is a required parameter, which specifies the year of the calendar.
  • month: It is a required parameter, which specifies the month of the calendar.

Return Value

The return type of this method is <class 'list'>, it returns a list of the weeks in the given month as full weeks, ie, each week consists of 7 values. Here days which have 0 values means they are out of that month.

Example of Calendar.monthdayscalendar() Method in Python

# Python program to illustrate the 
# use of monthdayscalendar() method

# import class
import calendar

# Creating Calendar Instance
cal = calendar.Calendar()
# default firstweekday =0
year = 2011
month = 11

print("Days outside of the month are 0")
print("Weekwise calendar of November 2011 with first weekday as Monday")
print(cal.monthdayscalendar(year, month))
print()
# always full weeks are listed.

# set the firstweekday to 3
cal = calendar.Calendar(firstweekday = 3)
year = 1998
month = 8

print("Weekwise calendar of August 1998 with first weekday as Thursday")
print(cal.monthdayscalendar(year, month))
print()

Output

Days outside of the month are 0
Weekwise calendar of November 2011 with first weekday as Monday
[
    [0, 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, 0, 0, 0, 0]
]

Weekwise calendar of August 1998 with first weekday as Thursday
[
    [0, 0, 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, 0, 0]
]



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.