Calendar Module:
The calendar module allows you to output calendars like a program and includes extra calendar-related operations. Calendar module functions and classes make use of an idealized calendar, the current Gregorian calendar extended in both directions indefinitely.
monthdays2calendar() Method:
The monthdays2calendar() method returns a list of full weeks in the specified month of the year. Weeks are a list of seven tuples of day and weekday numbers.
Syntax:
monthdays2calendar(year, month)
Parameter Values:
year:Â This is required. It is a number. The year for which the calendar should be created.
month:Â This is required. It is a number. The month for which the calendar should be created.
Return Value: This function returns a list of weeks in the month.
Program for calendar monthdays2calendar() Method with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Example1: Using For Loop
Approach:
- Import calendar module using the import keyword.
- Give the year as static input and store it in a variable.
- Give the month as static input and store it in another variable.
- Call the Calendar() function and store it in another variable.
- Apply monthdays2calendar() method to the above calendar by passing the given year, month as the arguments and store it in another variable.
- Iterate in the above result using the for loop.
- Inside the loop, print the iterator value.
- The Exit of the Program.
Below is the implementation:
# Import calendar module using the import keyword. import calendar # Give the year as static input and store it in a variable. gvn_yr = 2020 # Give the month as static input and store it in another variable. gvn_mont = 4 # Call the Calendar() function and store it in another variable. calendr = calendar.Calendar() # Apply monthdays2calendar() method to the above calendar by passing the given year, # month as the arguments and store it in another variable. rslt = calendr.monthdays2calendar(gvn_yr, gvn_mont) # Iterate in the above result using the for loop. for itr in rslt: # Inside the loop, print the iterator value. print(itr)
Output:
[(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)] [(20, 0), (21, 1), (22, 2), (23, 3), (24, 4), (25, 5), (26, 6)] [(27, 0), (28, 1), (29, 2), (30, 3), (0, 4), (0, 5), (0, 6)]
Example2:
Approach:
- Import calendar module using the import keyword.
- Give the year as static input and store it in a variable.
- Give the month as static input and store it in another variable.
- Call the Calendar() function and store it in another variable.
- Apply monthdays2calendar() method to the above calendar by passing the given year, month as the arguments and store it in another variable.
- Print the above result.
- The Exit of the Program.
Below is the implementation:
# Import calendar module using the import keyword. import calendar # Give the year as static input and store it in a variable. gvn_yr = 2012 # Give the month as static input and store it in another variable. gvn_mont = 6 # Call the Calendar() function and store it in another variable. calendr = calendar.Calendar() # Apply monthdays2calendar() method to the above calendar by passing the given year, # month as the arguments and store it in another variable. rslt = calendr.monthdays2calendar(gvn_yr, gvn_mont) # Print the above result. print(rslt)
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), (0, 6)]]
Method #2: Using Built-in Functions (User Input)
Example1: Using For Loop
Approach:
- Import calendar module using the import keyword.
- Give the year as user input using the int(input()) function and store it in a variable.
- Give the month as user input using the int(input()) function and store it in another variable.
- Call the Calendar() function and store it in another variable.
- Apply monthdays2calendar() method to the above calendar by passing the given year, month as the arguments and store it in another variable.
- Iterate in the above result using the for loop.
- Inside the loop, print the iterator value.
- The Exit of the Program.
Below is the implementation:
# Import calendar module using the import keyword. import calendar # Give the year as user input using the int(input()) function and store it in a variable. gvn_yr = int(input("Enter some random year = ")) # Give the month as user input using the int(input()) function and store it in another variable. gvn_mont = int(input("Enter some random month = ")) # Call the Calendar() function and store it in another variable. calendr = calendar.Calendar() # Apply monthdays2calendar() method to the above calendar by passing the given year, # month as the arguments and store it in another variable. rslt = calendr.monthdays2calendar(gvn_yr, gvn_mont) # Iterate in the above result using the for loop. for itr in rslt: # Inside the loop, print the iterator value. print(itr)
Output:
Enter some random year = 2014 Enter some random month = 7 [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)] [(7, 0), (8, 1), (9, 2), (10, 3), (11, 4), (12, 5), (13, 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)]