Python Program for calendar firstweekday() 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.

firstweekday() Method:

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

The firstweekday() method is used to retrieve the current weekday setting to begin each week.

Syntax:

firstweekday()

Parameter Values: This method has no parameters.

Return Value: None

Program for calendar firstweekday() Method with Examples in Python

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

Example1:

Approach:

  • Import calendar module using the import keyword.
  • Call the firstweekday() function and print it.
  • Pass some random number as an argument to the setfirstweekday() function (to set the 3rd day as first weekday).
  • Print the first week day after setting to some random number by calling the firstweekday() function
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Call the firstweekday() function and print it.
print("The first weekday by default = ", calendar.firstweekday())
# Pass some random number as an argument to the setfirstweekday() function
# (to set the 3rd day as firstweekday)
calendar.setfirstweekday(3)
# Print the first week day after setting to some random number by calling the
# firstweekday() function
print("The first week day after setting to 3 = ", calendar.firstweekday())

Output:

The first weekday by default = 0
The first week day after setting to 3 = 3

Example2: Using the prmonth() function to demonstrate the operation of the firstweekday() method.

Approach:

  • Import calendar module using the import keyword.
  • Pass some random year, month, width, lines as argument to the prmonth() method for printing the calendar for specified year.
  • 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
# for printing the calendar for specified year.
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