« Calendar Module
year | Year number for which dates are required |
width | Optional , default =3 , list of months in a row |
yeardays2calendar() : Returns a list of months with full week and tuple with day number and week day number .
Return : List of months where months have weeks as elements. Each week have 7 tuples.
Tuple has two elements
Day number : 1 to 28, 29 , 30 or 31 based on the month year. 0 is returned for the days outside the year
Weekday number : 0 to 6 : , 0 is Monday ,1 is Tuesday, 6 is Sunday ( based on setting of firstweekday() )
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)]]]
Details of output
List of month rows ( Number of months in a row is specified by width number )
Each Month contains list of full weeks ( 4 to 6 weeks in a month )
Each week contains 7 days ( tuple )
You can check the output by removing the comment of different lines.
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.
With 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 6
We are getting two months in a row or each element ( item ). So the output of second line len(y[1])
is 2
The first item of first row is the month, so the the len function len(y[0][0])
returns the number of full weeks in the month as 5.
We know each week has 7 days. So the value len(y[0][0][0])
returns 7.
Each week has date tuples as element ( item ) so we can get the respective day , week day number as the elements of the tuple, so len(y[1][0][0][1])
gives us 2.
Day number and week day number
We are getting tuple as item of the week tuple, so we can display day number of the month and week day number ( 0 to 6 ) .
print("Day :",y[5][1][3][5][0],",Week Day:",y[5][1][3][5][1])
Output
Day : 26 ,Week Day: 5
«Calendar Module in Python itermonthdays()
itermonthdays2()
itermonthdays3()
← Subscribe to our YouTube Channel here