ADVERTISEMENT
ADVERTISEMENT

Python Calendar Class | monthdays2calendar() Method with Example

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

Python Calendar.monthdays2calendar() Method

Calendar.monthdays2calendar() 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 as full weeks. Weeks, given here are lists of seven tuples, where each tuple consists of the day number and the weekday number of that day. Since weeks are written as full weeks, days outside the month are represented as 0.

Module:

    import calendar

Class:

    from calendar import Calendar

Syntax:

    monthdays2calendar(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, where each tuple represents the day and date on that date.

Example:

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

# import class
import calendar

# Creating Calendar Instance
cal = calendar.Calendar()
year = 2018
month = 11

# Here first value is the day of the month
# and second value is the weekday number 
# where Monday is 0 till Sunday which is 6
print("Days outside of the month are 0")
print("Weekwise calendar of November 2018 with first weekday as Monday")
print(cal.monthdays2calendar(year, month))
print()
# Note tuples always start from firstweekday value
# Full weeks are listed.

# set the firstweekday to 1
cal = calendar.Calendar(firstweekday = 5)
year = 1994
month = 4

print("Weekwise calendar of November 2011 with first weekday as Saturday")
print(cal.monthdays2calendar(year, month))
print()

Output

Days outside of the month are 0
Weekwise calendar of November 2018 with first weekday as Monday
[[(0, 0), (0, 1), (0, 2), (1, 3), (2, 4), (3, 5), (4, 6)], [(5, 0), (6, 1), (7, 2), (8, 3), (9, 4), (10, 5), (11, 6)], [(12, 0), (13, 1), (14, 2), (15, 3), (16, 4), (17, 5), (18, 6)], [(19, 0), (20, 1), (21, 2), (22, 3), (23, 4), (24, 5), (25, 6)], [(26, 0), (27, 1), (28, 2), (29, 3), (30, 4), (0, 5), (0, 6)]]

Weekwise calendar of November 2011 with first weekday as Saturday
[[(0, 5), (0, 6), (0, 0), (0, 1), (0, 2), (0, 3), (1, 4)], [(2, 5), (3, 6), (4, 0), (5, 1), (6, 2), (7, 3), (8, 4)], [(9, 5), (10, 6), (11, 0), (12, 1), (13, 2), (14, 3), (15, 4)], [(16, 5), (17, 6), (18, 0), (19, 1), (20, 2), (21, 3), (22, 4)], [(23, 5), (24, 6), (25, 0), (26, 1), (27, 2), (28, 3), (29, 4)], [(30, 5), (0, 6), (0, 0), (0, 1), (0, 2), (0, 3), (0, 4)]]
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.