Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

communitycreator
python

What is itermonthdays() method from calendar in Python?

Fouzia Bashir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The itermonthdays() method returns an iterator of a specified month and year. Days returned will be day number.

This method is from the calendar module. It is similar to itermonthdates().

Syntax


itermonthdays(year, month)

Parameters

This method takes two argument values:

  • year: year of the calendar.
  • month: month of the calendar.

Return value

iterator: It returns an iterator of the specified month. For the days other than the specified month, the day number is 0.

We can use Python’s iter() method to create an iterator instance.

Example 1

# Example#1
# including calendar module
from calendar import Calendar
# calendar instance
calendarObject = Calendar()
# iterating with itermonthdays
for day in calendarObject.itermonthdays(2022, 1):
print(day)

Explanation

  • Line 3: We load the calendar module in the program.
  • Line 5: We instantiate the Calendar class from the calendar module.
  • Line 7: We use the itermonthdays() method, with year=2022 and month=1, to print the number of days in the specified month.

Example 2

# Example#2
# including calendar module
from calendar import Calendar
# defining argument values
year = 2021
month = 4 # April
# creating calendar instance
obj = Calendar()
# iterating with itermonthdays
for day in obj.itermonthdays(year, month):
print(day)

Explanation

  • Line 3: We include the calendar module in the program.
  • Lines 5-6: We set the year=2021 and month=4.
  • Line 8: We instantiate the Calendar class from the calendar module.
  • Line 10: We use the itermonthdays() method to print the number of days in the specified month and year to the console.

RELATED TAGS

communitycreator
python

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring