year | Year number for which dates are required |
width | Optional , default =3 , list of months in a row |
import calendar
my_cal= calendar.Calendar()
for x in my_cal.yeardays2calendar(2020):
print(x)
The output is list of rows of months with full weeks and above tuples. 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), (0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)],
[(6, 0), (7, 1), (8, 2), (9, 3), (10, 4), (11, 5), (12, 6)],
[(13, 0), (14, 1), (15, 2), (16, 3), (17, 4), (18, 5), (19, 6)],
--------------
--------------
[(14, 0), (15, 1), (16, 2), (17, 3), (18, 4), (19, 5), (20, 6)],
[(21, 0), (22, 1), (23, 2), (24, 3), (25, 4), (26, 5), (27, 6)],
[(28, 0), (29, 1), (30, 2), (31, 3), (0, 4), (0, 5), (0, 6)]]]
import calendar
my_cal= calendar.Calendar()
y= my_cal.yeardays2calendar(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][0]) # list of days tuple of first week
## 5th date tuple of the fourth week of 2nd month of 6th row
#print(y[5][1][3][5]) # (26,5)
print("Day :",y[5][1][3][5][0],",Week Day:",y[5][1][3][5][1])
Output ( last line only )
Day : 26 ,Week Day: 5
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.yeardays2calendar(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 tuple in first month, first week:",len(y[0][0][0]))
print("No. of items in tuple of Second day : ",len(y[1][0][0][1]))
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 tuple in first month, first week: 7
No. of items in tuple of Second day : 2
In above code the line y= my_cal.yeardays2calendar(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.
len(y[1][0][0][1])
gives us 2.
print("Day :",y[5][1][3][5][0],",Week Day:",y[5][1][3][5][1])
Output
Day : 26 ,Week Day: 5
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.