Python Program for calendar setfirstweekday() Method with Examples

Calendar Module:

The calendar module allows you to output calendars like a program and includes extra calendar-related operations. Calendar module functions and classes make use of an idealized calendar, the current Gregorian calendar extended in both directions indefinitely.

setfirstweekday() Method:

calendar.setfirstweekday(weekday) is a function in Python’s calendar module for creating simple text calendars.

The setfirstweekday() method sets the weekday (0 is Monday, 6 is Sunday) to start each week.
The values MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, and SUNDAY are provided for convenience.

Syntax:

setfirstweekday()

Parameter Values: This method has no parameters.

Return Value: None

Program for calendar setfirstweekday() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import calendar module using the import keyword.
  • Set the day as SUNDAY, and pass it as an argument to the setfirstweekday() function.
  • Store it in a variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Set the day as SUNDAY, and pass it as an argument to the setfirstweekday() function
# Store it in a variable.
rslt_calndr = calendar.setfirstweekday(calendar.SUNDAY)
# Print the above result.
print(rslt_calndr)

Output:

None

Using the prmonth() function to demonstrate the operation of the setfirstweekday() method.

Reference : Python Program for calendar prmonth() Method with Examples

Approach:

  • Import calendar module using the import keyword.
  • Pass some random year, month, width, lines as argument to the prmonth() method.
  • Set the first week day number using the setfirstweekday() method.
  • Verify the changed day using the firstweekday() method and print it.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Pass some random year, month, width, lines as argument to the prmonth() method
print("The calendar of May 2005 : ")
calendar.prmonth(2005, 5, 3, 1)
# Set the first week day number using the setfirstweekday() method.
calendar.setfirstweekday(3)
print("\r")
# Verify the changed day using the firstweekday() method and print it.
print("The changed first week day digit = ", end="")
print(calendar.firstweekday())

Output:

The calendar of May 2005 : 
          May 2005
Mon Tue Wed Thu Fri Sat Sun
                          1
  2   3   4   5   6   7   8
  9  10  11  12  13  14  15
 16  17  18  19  20  21  22
 23  24  25  26  27  28  29
 30  31

The changed first week day digit = 3