ADVERTISEMENT
ADVERTISEMENT

Python Calendar Class | monthdayscalendar() Method with Example

Python Calendar.monthdayscalendar() Method: Here, we are going to learn about the monthdayscalendar() method of Calendar class in Python with its definition, syntax, and examples.
Submitted by Hritika Rajput, on May 10, 2020

Python Calendar.monthdayscalendar() Method

Calendar.monthdayscalendar() method is an inbuilt method of the Calendar class of calendar module in Python. It uses an instance of this class and 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:

    import calendar

Class:

    from calendar import Calendar

Syntax:

    monthdayscalendar(year, month)

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:

# 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]
]
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.