ADVERTISEMENT
ADVERTISEMENT

Python Calendar Class | itermonthdays2() Method with Example

Python Calendar.itermonthdays2() Method: Here, we are going to learn about the itermonthdays2() method of Calendar class in Python with its definition, syntax, and examples.
Submitted by Hritika Rajput, on May 10, 2020

Python Calendar.itermonthdays2() Method

Calendar.itermonthdays2() method is an inbuilt method of the Calendar class of calendar module in Python. It uses an instance of this class and returns an iterator for the given month in the given year. Days returned are tuples where the first value is the day of the month and the second value is the weekday number, ie, day of the week on that date. The function returns full weeks, ie, each week in the month will have 7 tuples and the dates outside of the month will have 0 value in the first value of the tuple.

For example, (0, 3) this means it is outside of that month and it is Thursday on this day.

Module:

    import calendar

Class:

    from calendar import Calendar

Syntax:

    itermonthdays2(year, month)

Parameter(s):

  • year: It is a required parameter, which specifies the year of the calendar.
  • month: It is a required parameter, which specifies the month of the calendar.

Return value:

The return type of this method is <class 'generator'>, it returns an iterator for the given month of the given year. The day returned is a tuple representing the date and weekday on that given day. Also, remember that full weeks are printed.

Example:

# Python program to illustrate the 
# use of itermonthdays2() method

# import class
import calendar

# Creating Calendar Instance
cal = calendar.Calendar()
year = 2009
month = 2

print("Iterating over the weeks of February 2009 where each tuple is date and weekday on that day")
print("Days outside the month are written as 0")
print("Iteration starts from weekday/second value as 0, ie, Monday")
for i in cal.itermonthdays2(year, month):
    print(i)
# first value is the day of the month; 
# days outside of the month is 0
# second value is weekday number where 
# Monday is 0 till Sunday which is 6
print()
print()

# set the firstweekday to 4
cal = calendar.Calendar(firstweekday = 4)
year = 2020
month = 1

print("Iterating over the weeks of January 2020 where each tuple is date and weekday on that day")
print("Iteration starts from weekday/second value as 4, ie, Friday")
for i in cal.itermonthdays2(year, month):
    print(i)
print()
print()

Output

Iterating over the weeks of February 2009 where each tuple is date and weekday on that day
Days outside the month are written as 0
Iteration starts from weekday/second value as 0, ie, Monday
(0, 0)
(0, 1)
(0, 2)
(0, 3)
(0, 4)
(0, 5)
(1, 6)
(2, 0)
(3, 1)
(4, 2)
(5, 3)
(6, 4)
(7, 5)
(8, 6)
(9, 0)
(10, 1)
(11, 2)
(12, 3)
(13, 4)
(14, 5)
(15, 6)
(16, 0)
(17, 1)
(18, 2)
(19, 3)
(20, 4)
(21, 5)
(22, 6)
(23, 0)
(24, 1)
(25, 2)
(26, 3)
(27, 4)
(28, 5)
(0, 6)


Iterating over the weeks of January 2020 where each tuple is date and weekday on that day
Iteration starts from weekday/second value as 4, ie, Friday
(0, 4)
(0, 5)
(0, 6)
(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)
(31, 4)
(0, 5)
(0, 6)
(0, 0)
(0, 1)
(0, 2)
(0, 3)
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.