Python Calendar iterweekdays() Method with Example

Python Calendar.iterweekdays() Method: In this tutorial, we will learn about the iterweekdays() method of Calendar class in Python with its usage, syntax, and examples. By Hritika Rajput Last updated : April 24, 2023

Python Calendar.iterweekdays() Method

The Calendar.iterweekdays() method is an inbuilt method of the Calendar class of calendar module, it returns an iterator for the weekday numbers that are used for one week. The first number that the iterator returns is the first weekday set for that instance. By default, the first-weekday set is 0 which is Monday.

Module

The following module is required to use iterweekdays() method:

import calendar

Class

The following class is required to use iterweekdays() method:

from calendar import Calendar

Syntax

The following is the syntax of iterweekdays() method:

    iterweekdays()

Parameter(s)

The following are the parameter(s):

  • None

Return Value

The return type of this method is <class 'generator'>, it returns an iterator for the weekday numbers giving 7 weekday numbers starting from that iterator.

Example of Calendar.iterweekdays() Method in Python

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

# import module
import calendar

# Creating Calendar instance
cal = calendar.Calendar()
print("Iterating with first weekday as 0, ie, Monday")
for i in cal.iterweekdays():
    print(i)
print()

# Changing firstweekday for the Calendar
cal  = calendar.Calendar(firstweekday = 3)
# iterator starts with 3
print("Iterating with first weekday as 3, ie, Thursday")
for i in cal.iterweekdays():
    print(i)
print()

Output

Iterating with first weekday as 0, ie, Monday
0
1
2
3
4
5
6

Iterating with first weekday as 3, ie, Thursday
3
4
5
6
0
1
2




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.