Python calendar iterweekdays function example

Introduction :

iterweekdays() is a function defined in python calendar standard library. It returns one iterator for the weekdays numbers. The first value is the first-week day value that can be retrieved by firstweekday().

Example program :

In this example, I will show you how to use iterweekdays() and how its values are changed if we change the firstweekday :

import calendar

cal = calendar.Calendar()

for day in cal.iterweekdays():
    print(day)

print('--------')
cal.setfirstweekday(2)
for day in cal.iterweekdays():
    print(day)

Here, we have created one Calendar object cal first. Using a for..in loop, we iterated through its values and printed them. Then, we changed the firstweekday to 2 using setfirstweekday() function and iterate through the values again.

It prints the below output :

0
1
2
3
4
5
6
--------
2
3
4
5
6
0
1

You can see that the start day was 0 by default and it changed to 2. iterweekdays() always starts from the first week day.

Similar tutorials :