Python calendar monthdays2calendar method examples

Python calendar monthdays2calendar example :

Python calendar monthdays2calendar returns one list of weeks in a specific month and year. It returns the data as full weeks. Each data is a tuple holding day and weekday values.

Syntax of monthdays2calendar :

monthdays2calendar is defined as below :

monthdays2calendar(year, month)

It will return the list for the given year and month.

Example program :

Let’s take a look at the below example :

import calendar

cal = calendar.Calendar()

print(cal.monthdays2calendar(2020, 5))

It will print the below output :

[[(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), (31, 6)]]

We are getting the list of May, 2020. The first four values have 0 because 1st May,2020 starts on Friday. It returns the remaining days for that week as well. This month ends on Sunday, so we don’t have any extra trailing values.

Let’s check one more example for Oct, 2020 :

import calendar

cal = calendar.Calendar()

print(cal.monthdays2calendar(2020, 10))

It will print :

[[(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), (31, 5), (0, 6)]]

October, 2020 starts on Thursday and ends on Saturday. So, we have three zero values in the beginning and one at end.