Python calendar module | monthdays2calendar() method
Calendar module allows to output calendars like program, and provides additional useful functions related to the calendar. Functions and classes defined in Calendar module use an idealized calendar, the current Gregorian calendar extended indefinitely in both directions.
monthdays2calendar() method in Python is used to get a list of the weeks in the month of the year as full weeks.
Syntax: monthdays2calendar(year, month) Parameter: year: year of the calendar month: month of the calendar Returns: a list of the weeks in the month.
Code #1:
# Python program to demonstrate working of # monthdays2calendar() method # importing calendar module import calendar obj = calendar.Calendar() year = 2018mnth = 9 # priting with monthdays2calendar print(obj.monthdays2calendar(year, mnth)) |
Output:
[[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 5), (2, 6)], [(3, 0), (4, 1), (5, 2), (6, 3), (7, 4), (8, 5), (9, 6)], [(10, 0), (11, 1), (12, 2), (13, 3), (14, 4), (15, 5), (16, 6)], [(17, 0), (18, 1), (19, 2), (20, 3), (21, 4), (22, 5), (23, 6)], [(24, 0), (25, 1), (26, 2), (27, 3), (28, 4), (29, 5), (30, 6)]]
Note that weeks in the output are lists of seven-tuples of day numbers and weekday numbers.
Code #2: iterating the list of weeks
# Python program to demonstrate working of # monthdays2calendar() method # importing calendar module import calendar obj = calendar.Calendar() # iteratign with monthdays2calendar for day in obj.monthdays2calendar(2018, 9): print(day) |
Output:
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 5), (2, 6)] [(3, 0), (4, 1), (5, 2), (6, 3), (7, 4), (8, 5), (9, 6)] [(10, 0), (11, 1), (12, 2), (13, 3), (14, 4), (15, 5), (16, 6)] [(17, 0), (18, 1), (19, 2), (20, 3), (21, 4), (22, 5), (23, 6)] [(24, 0), (25, 1), (26, 2), (27, 3), (28, 4), (29, 5), (30, 6)]
Recommended Posts:
- Python calendar module | pryear() method
- Python calendar module | leapdays() method
- Python calendar module | itermonthdates() method
- Python calendar module | setfirstweekday() method
- Python calendar module | yeardayscalendar() method
- Python calendar module | yeardays2calendar() method
- Python calendar module | formatmonth() method
- Python calendar module | firstweekday() method
- Python calendar module | formatyear() method
- Python calendar module | isleap() method
- Python calendar module | prmonth() method
- Python calendar module | monthdatescalendar() method
- Python calendar module | itermonthdays() method
- Python calendar module | iterweekdays() method
- Python calendar module | itermonthdays2() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



