Python Program for calendar itermonthdays2() 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.

itermonthdays2() Method:

The itermonthdays2() method, returns an iterator for the month in the year like itermonthdates(). Days will be returned as tuples of a day number and a week day number.

Syntax:

itermonthdays2(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: Iterator for the month is returned.

Program for calendar itermonthdays2() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Example1:

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 itermonthdays2() 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 itermonthdays2() method to the above calendar by passing the given year,
# month as the arguments and store it in another variable.
rslt = calendr.itermonthdays2(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 by setting firstweekday=2 and store it in another variable.
  • Apply itermonthdays2() 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 = 2011
# Give the month as static input and store it in another variable.
gvn_mont = 6
# Call the Calendar() function by setting firstweekday=2 and store it in
# another variable.
calendr = calendar.Calendar(firstweekday=2)
# Apply itermonthdays2() method to the above calendar by passing the given year,
# month as the arguments and store it in another variable.
rslt = calendr.itermonthdays2(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:

(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)
(0, 0)
(0, 1)

Method #2: Using Built-in Functions (User Input)

Example1:

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 itermonthdays2() 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 itermonthdays2() method to the above calendar by passing the given year,
# month as the arguments and store it in another variable.
rslt = calendr.itermonthdays2(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 = 2003
Enter some random month = 2
(0, 0)
(0, 1)
(0, 2)
(0, 3)
(0, 4)
(1, 5)
(2, 6)
(3, 0)
(4, 1)
(5, 2)
(6, 3)
(7, 4)
(8, 5)
(9, 6)
(10, 0)
(11, 1)
(12, 2)
(13, 3)
(14, 4)
(15, 5)
(16, 6)
(17, 0)
(18, 1)
(19, 2)
(20, 3)
(21, 4)
(22, 5)
(23, 6)
(24, 0)
(25, 1)
(26, 2)
(27, 3)
(28, 4)
(0, 5)
(0, 6)