year | Year number for which dates are requried |
width | Optional , default =3 , list of months in a row |
import calendar
my_cal= calendar.Calendar()
for x in my_cal.yeardayscalendar(2020):
print(x)
The output is list of rows of months with full weeks and day number as items. Based on the setting of firstweekday(), the first week of the year will start from previous year, last week and will extend up to next year’s first week.
[[[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]],
[[0, 0, 0, 0, 0, 1, 2], [3, 4, 5, 6, 7, 8, 9],
-------------
-------------
-------------
[[0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 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, 0]]]
import calendar
my_cal= calendar.Calendar()
y= my_cal.yeardayscalendar(2020,2)
#print(y[5]) # list of month rows , it has 2 months
#print(y[5][1]) # list of full weeks of 2nd month of 6th row
#print(y[5][1][3]) # list of days of fourth week
## 5th item of the fourth week of 2nd month of 6th row
#print(y[5][1][3][5]) # 26
print("Day :",y[5][1][3][5])
Output ( last line only )
Day : 26
We are getting output as list. So length or number of elements ( items ) present in the list ( iterable object) we can get by using len() function.
import calendar
my_cal= calendar.Calendar()
y= my_cal.yeardayscalendar(2020,2)
print("No. of rows of months : ", len(y)) # 6
print("No. of months in the row : ", len(y[1])) # 2
print("No. of full weeks in first month :",len(y[0][0])) # 5
print("No. of days in first month, first week:",len(y[0][0][0]))
Output
No. of rows of months : 6
No. of months in the row : 2
No. of full weeks in first month : 5
No. of days in first month, first week: 7
In above code the line y= my_cal.yeardayscalendar(2020,2) set the width to 2. width=2
we will get 6 elements or items having 2 months each. ( 12 month in a year ) . So the output of first line len(y)
is 6len(y[1])
is 2 len(y[0][0])
returns the number of full weeks in the month as 5.
len(y[0][0][0])
returns 7.
print("Day :",y[5][1][3][5])
Output
Day : 26
Calendar Module in Python itermonthdays()
itermonthdays2()
itermonthdays3()
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.