Python itermonthdays2(): Day and Weekday Tuples

calendar.Calendar.itermonthdays2(year, month) returns an iterator of two-item tuples. Each tuple contains (day_number, weekday_number). For dates outside the chosen month, the day number is 0; weekday numbers still identify Monday through Sunday.

import calendar
my_cal= calendar.Calendar()
for x in my_cal.itermonthdays2(2020,7):
    print(x)

Output excerpt:

(0, 0)
(0, 1)
(1, 2)
(2, 3)
(3, 4)
(4, 5)
(5, 6)
(6, 0)
(7, 1)
(8, 2)
... (middle rows omitted) ...
(30, 3)
(31, 4)
(0, 5)
(0, 6)

In July 2020, the first two tuples are (0, 0) and (0, 1) because Monday 29 June and Tuesday 30 June fall outside July. The first July date is (1, 2): Wednesday 1 July. The returned rows cover full weeks, not just July dates.

Practice Python itermonthdays2()

Run the July 2020 examples, change the starting weekday, unpack tuples, filter zero values, and test your answers.

Open Python itermonthdays2 practice notebook in Google Colab View notebook on GitHub

The notebook uses only Python's standard library. The links become active after the notebook is uploaded to GitHub.

On this page — Show page sections (jump to a topic)— Hide page sections

Syntax and parameters

Create a calendar.Calendar() object and call itermonthdays2(year, month). The method accepts the year first, followed by a month from 1 to 12. firstweekday is a setting on the Calendar object—not a third argument to this method.

ItemMeaningExample
yearYear to display2020
monthMonth number, 1 to 127
firstweekdayStart of the calendar week; Monday is 0, Sunday is 6Calendar(firstweekday=3)
(day, weekday)Two-item tuple; day=0 marks an adjacent-month slot(1, 2) = Wednesday 1 July
import calendar
values = calendar.Calendar().itermonthdays2(2020, 7)
print(type(values).__name__)
print(next(values))
print(next(values))

Output:

generator
(0, 0)
(0, 1)

The result is an iterator, not a ready-made list. Each value it yields is a two-item tuple.

Start the week on Thursday

The original lesson changes the first weekday to 3 (Thursday). Because July 1, 2020 was Wednesday, the calendar then needs six full rows instead of five. The weekday number assigned to a given date does not change. See setting the first weekday.

import calendar
my_cal= calendar.Calendar(firstweekday=3)
for x in my_cal.itermonthdays2(2020,7):
    print(x)

Output excerpt:

(0, 3)
(0, 4)
(0, 5)
(0, 6)
(0, 0)
(0, 1)
(1, 2)
(2, 3)
(3, 4)
(4, 5)
... (middle rows omitted) ...
(0, 6)
(0, 0)
(0, 1)
(0, 2)

This retains the Thursday-first example. Notice that (1, 2) still marks Wednesday July 1; only the sequence of displayed week slots changes.

Read tuple items by index

You can access the day number with x[0] and the weekday number with x[1]. This preserves the original demonstration, while keeping the output excerpt manageable.

import calendar
my_cal= calendar.Calendar(firstweekday=3)
for x in my_cal.itermonthdays2(2020,7):
    print('day:',x[0],', weekday : ',x[1])

Output excerpt:

day: 0 , weekday :  3
day: 0 , weekday :  4
day: 0 , weekday :  5
day: 0 , weekday :  6
day: 0 , weekday :  0
day: 0 , weekday :  1
day: 1 , weekday :  2
day: 2 , weekday :  3
day: 3 , weekday :  4
... (middle rows omitted) ...
day: 0 , weekday :  6
day: 0 , weekday :  0
day: 0 , weekday :  1
day: 0 , weekday :  2

Unpack each tuple directly

Tuple unpacking makes code easier to read than repeatedly using numeric indexes. Every item contains exactly two values.

import calendar
my_cal = calendar.Calendar()
for day, weekday in my_cal.itermonthdays2(2020, 7):
    if day:
        print(f"Day {day}: weekday {weekday}")

Output excerpt:

Day 1: weekday 2
Day 2: weekday 3
Day 3: weekday 4
Day 4: weekday 5
Day 5: weekday 6
Day 6: weekday 0
Day 7: weekday 1
... (middle rows omitted) ...
Day 28: weekday 1
Day 29: weekday 2
Day 30: weekday 3
Day 31: weekday 4

Filter out adjacent-month placeholders

Skip entries where day == 0 to work only with dates inside the selected month. The iterator includes zero placeholders so that its length is a multiple of seven.

import calendar
values = calendar.Calendar().itermonthdays2(2020, 7)
real_days = [(day, weekday) for day, weekday in values if day != 0]
print('First:', real_days[0])
print('Last:', real_days[-1])
print('Dates in July:', len(real_days))

Output:

First: (1, 2)
Last: (31, 4)
Dates in July: 31

Convert weekday numbers to names

Python weekday numbers always run from Monday 0 through Sunday 6. Use calendar.day_name[weekday] for a readable name, regardless of the first weekday setting.

import calendar
my_cal = calendar.Calendar(firstweekday=3)
for day, weekday in my_cal.itermonthdays2(2020, 7):
    if 1 <= day <= 5:
        print(day, calendar.day_name[weekday])

Output:

1 Wednesday
2 Thursday
3 Friday
4 Saturday
5 Sunday

Create a weekday lookup for a month

Create a dictionary from day number to weekday name. This is useful for checking which weekday a chosen date falls on without displaying every calendar slot.

import calendar
lookup = {day: calendar.day_name[weekday]
          for day, weekday in calendar.Calendar().itermonthdays2(2020, 7)
          if day != 0}
print('July 1:', lookup[1])
print('July 15:', lookup[15])
print('July 31:', lookup[31])

Output:

July 1: Wednesday
July 15: Wednesday
July 31: Friday

Find Saturdays and Sundays

Weekends have weekday numbers 5 and 6. Filter zero placeholders before collecting the weekend dates. These values are independent of firstweekday.

import calendar
values = calendar.Calendar(firstweekday=3).itermonthdays2(2020, 7)
weekends = [(day, calendar.day_abbr[weekday])
            for day, weekday in values if day and weekday >= 5]
print(weekends)
print('Weekend days:', len(weekends))

Output:

[(4, 'Sat'), (5, 'Sun'), (11, 'Sat'), (12, 'Sun'), (18, 'Sat'), (19, 'Sun'), (25, 'Sat'), (26, 'Sun')]
Weekend days: 8

Count Monday–Friday working days

Weekdays are numbered 0 to 4. Count those dates for a simple Monday–Friday work schedule; this does not account for public holidays.

import calendar
values = calendar.Calendar().itermonthdays2(2020, 7)
workdays = [day for day, weekday in values if day != 0 and weekday < 5]
print('Working days:', len(workdays))
print('First five:', workdays[:5])
print('Last five:', workdays[-5:])

Output:

Working days: 23
First five: [1, 2, 3, 6, 7]
Last five: [27, 28, 29, 30, 31]

Group the iterator into complete weeks

Although itermonthdays2() returns a flat iterator, every seven consecutive tuples form one complete week. Converting it to a list lets you slice these groups for a calendar table.

import calendar
slots = list(calendar.Calendar().itermonthdays2(2020, 7))
for index in range(0, len(slots), 7):
    print('Week', index // 7 + 1, ':', slots[index:index + 7])

Output:

Week 1 : [(0, 0), (0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
Week 2 : [(6, 0), (7, 1), (8, 2), (9, 3), (10, 4), (11, 5), (12, 6)]
Week 3 : [(13, 0), (14, 1), (15, 2), (16, 3), (17, 4), (18, 5), (19, 6)]
Week 4 : [(20, 0), (21, 1), (22, 2), (23, 3), (24, 4), (25, 5), (26, 6)]
Week 5 : [(27, 0), (28, 1), (29, 2), (30, 3), (31, 4), (0, 5), (0, 6)]

Count calendar rows and actual dates

A month view uses four, five, or six rows of seven slots. Do not confuse the total number of slots with the number of days in the month.

import calendar
slots = list(calendar.Calendar(firstweekday=3).itermonthdays2(2020, 7))
print('Week rows:', len(slots) // 7)
print('Calendar slots:', len(slots))
print('Real dates:', sum(day != 0 for day, weekday in slots))

Output:

Week rows: 6
Calendar slots: 42
Real dates: 31

Start the calendar on Sunday

With firstweekday=calendar.SUNDAY, the calendar starts each week on Sunday (weekday 6). Wednesday remains weekday 2 in every tuple.

import calendar
my_cal = calendar.Calendar(firstweekday=calendar.SUNDAY)
slots = list(my_cal.itermonthdays2(2020, 7))
print('First week:', slots[:7])
print('July 1:', next((day, wd) for day, wd in slots if day == 1))

Output:

First week: [(0, 6), (0, 0), (0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]
July 1: (1, 2)

An iterator can be consumed only once

After you iterate through every item, the same iterator is exhausted. Create a new iterator or store its values in a list if you need to access them again.

import calendar
it = calendar.Calendar().itermonthdays2(2020, 7)
first_pass = list(it)
second_pass = list(it)
print('First pass:', len(first_pass))
print('Second pass:', len(second_pass))

Output:

First pass: 35
Second pass: 0

Compare related calendar methods

itermonthdates() yields complete datetime.date objects, while itermonthdays() yields only integer day numbers. itermonthdays2() supplies a pair of integers. The newer itermonthdays3() and itermonthdays4() include a year and month for full calendar dates.

MethodTypical itemBest for
itermonthdays()0 or 1Only day numbers
itermonthdays2()(1, 2)Day + weekday number
itermonthdates()datetime.date(2020, 7, 1)Full dates across month boundaries
itermonthdays3()(2020, 7, 1)Year, month, day integer tuple
itermonthdays4()(2020, 7, 1, 2)Year, month, day, weekday tuple
import calendar
cal = calendar.Calendar()
print(next(cal.itermonthdays(2020, 7)))
print(next(cal.itermonthdays2(2020, 7)))
print(next(cal.itermonthdates(2020, 7)))
print(next(cal.itermonthdays3(2020, 7)))
print(next(cal.itermonthdays4(2020, 7)))

Output:

0
(0, 0)
2020-06-29
(2020, 6, 29)
(2020, 6, 29, 0)

Compare itermonthdays2() with monthdays2calendar()

Use monthdays2calendar() when you want the values already arranged into week lists. Both functions yield the same tuples in the same order, but one is a flat iterator and the other is a nested list.

import calendar
cal = calendar.Calendar(firstweekday=3)
flat = list(cal.itermonthdays2(2020, 7))
rows = cal.monthdays2calendar(2020, 7)
print('Same data:', flat == [item for row in rows for item in row])
print('Week rows:', len(rows))
print('First week:', rows[0])

Output:

Same data: True
Week rows: 6
First week: [(0, 3), (0, 4), (0, 5), (0, 6), (0, 0), (0, 1), (1, 2)]

Check February in a leap year

A February 2024 calendar has 29 real dates because 2024 is a leap year. As usual, adjacent-month placeholders have day number zero.

import calendar
slots = list(calendar.Calendar().itermonthdays2(2024, 2))
real = [(day, weekday) for day, weekday in slots if day]
print('Leap year:', calendar.isleap(2024))
print('Days:', len(real))
print('February 29:', real[-1])

Output:

Leap year: True
Days: 29
February 29: (29, 3)

Use the tuples in a simple task planner

Pair the day number with its weekday label before scheduling work. The following example prints a weekday plan for the first seven actual dates in July.

import calendar
cal = calendar.Calendar()
plan = []
for day, weekday in cal.itermonthdays2(2020, 7):
    if day and weekday < 5 and day <= 10:
        plan.append(f'July {day} ({calendar.day_abbr[weekday]}): Review tasks')
for task in plan:
    print(task)

Output:

July 1 (Wed): Review tasks
July 2 (Thu): Review tasks
July 3 (Fri): Review tasks
July 6 (Mon): Review tasks
July 7 (Tue): Review tasks
July 8 (Wed): Review tasks
July 9 (Thu): Review tasks
July 10 (Fri): Review tasks

Common mistakes and troubleshooting

  • Interpreting 0 as the first day: zero means the calendar slot belongs to the adjacent month.
  • Confusing weekday with the day of the month: in (1, 2), day 1 is Wednesday (weekday 2).
  • Assuming Friday is 4 only on Friday-first calendars: weekday numbering never changes; only iteration order changes.
  • Expecting a list of weeks: itermonthdays2() produces a flat iterator; use monthdays2calendar() for week lists.
  • Accessing an exhausted iterator: convert it to a list before repeatedly traversing it.
  • Using an invalid month: month must be between 1 and 12; calendar.IllegalMonthError can be caught.
import calendar
try:
    values = list(calendar.Calendar().itermonthdays2(2020, 13))
except calendar.IllegalMonthError:
    print('Please choose a month between 1 and 12')

Output:

Please choose a month between 1 and 12

Practice exercises

Open the Colab notebook to complete the exercises and use its assertions to check your results:

  • Print the tuple for the date July 15, 2020, and show its weekday name.
  • Start July 2020 on Sunday and check whether July 1 has a different weekday number.
  • Count the Saturdays and Sundays in February 2024.
  • Group July 2020 into rows of seven and show the weekday names above each column.
  • Compare itermonthdays2() with monthdays2calendar() for the same month.

Frequently asked questions

What does itermonthdays2() return?

It yields tuples of two integers: (day_of_month, weekday_number). It does not return datetime.date objects.

Why does it include (0, weekday) tuples?

Complete weeks often include days from the previous or next month. The method represents those dates with a day number of zero.

Does firstweekday change the weekday numbers?

No. Monday is always 0 and Sunday is always 6; changing firstweekday changes the order and sometimes the number of complete week rows.

What is the difference between itermonthdays2() and monthdays2calendar()?

Both return the same two-number tuples, but itermonthdays2() yields them one at a time, while monthdays2calendar() returns a list of complete week lists.

Can I get the year and month as well?

Use itermonthdays4() for (year, month, day, weekday) tuples, or itermonthdates() for date objects.

Back to top ↑




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer