iterweekdays()

iterweekdays() : Returns an Iterator having week day in numbers.

Starting number is 0 ( default number for Monday ) but can be changed based on setting of setfirstweekday()
import calendar
my_cal= calendar.Calendar()
for x in my_cal.iterweekdays():
	print(x)
Output
0
1
2
3
4
5
6
We can set first week day to Friday ( firstweekday=4)
import calendar
my_cal= calendar.Calendar(firstweekday=4)
for x in my_cal.iterweekdays():
	print(x)
Output
4
5
6
0
1
2
3

Use Case: Task Scheduling

In task scheduling, we can iterate through weekdays to assign tasks based on the working days:

import calendar
my_cal = calendar.Calendar(firstweekday=0)  # Starting with Monday
for day in my_cal.iterweekdays():
    print("Assign task for day:", day)
Output
Assign task for day: 0
Assign task for day: 1
Assign task for day: 2
Assign task for day: 3
Assign task for day: 4
Assign task for day: 5
Assign task for day: 6

Setting Different Start Days

By changing the first weekday, you can create calendars starting from any day:

my_cal = calendar.Calendar(firstweekday=2)  # Start with Wednesday
for day in my_cal.iterweekdays():
    print(day)
Output
2
3
4
5
6
0
1

Calendar Module in Python



Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    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 FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer