Python Program for calendar monthdayscalendar() Method with Examples

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.

monthdayscalendar() Method:

The monthdayscalendar() method returns a list of full weeks in the specified month of the year. Weeks are a list of seven-day numbers.

Syntax:

monthdayscalendar(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 monthdayscalendar() 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 monthdayscalendar() 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 monthdayscalendar() method to the above calendar by passing the given year,
# month as the arguments and store it in another variable.
rslt = calendr.monthdayscalendar(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, 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]

Note:

It's worth noting that the weeks in the output are simply lists of seven-day
numbers.

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 monthdayscalendar() 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 monthdayscalendar() method to the above calendar by passing the given year,
# month as the arguments and store it in another variable.
rslt = calendr.monthdayscalendar(gvn_yr, gvn_mont)
# Print the above result.
print(rslt)

Output:

[[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]]

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 monthdayscalendar() 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 monthdayscalendar() method to the above calendar by passing the given year,
# month as the arguments and store it in another variable.
rslt = calendr.monthdayscalendar(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 = 2013
Enter some random month = 2
[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, 0, 0, 0]