Calendar Functions in Python -(monthrange(), prcal(), weekday()?)

PythonServer Side ProgrammingProgramming

We are going to explore different methods of calendar module in this tutorial. Let's see one by one.

calendar.monthrange(year, month)

The method calendar.monthrange(year, month) returns starting weekday number and number of days of the given month. It returns two values in a tuple. Let's see one example.

Example

 Live Demo

# importing the calendar module
import calendar
# initializing year and month
year = 2019
month = 1
# getting the tuple of weekday and no. of days
weekday, no_of_days = calendar.monthrange(year, month)
print(f'Weekday number: {weekday}')
print(f'No. of days: {no_of_days}')

Output

If you run the above code, you will get the following results.

Weekday number: 1
No. of days: 31

calendar.prcal(year)

The method calendar.prcal(year) prints the calendar of the year without print function.

Example

 Live Demo

# importing the calendar module
import calendar
# initializing year
year = 2019
# printing the calendar using prcal() method
calendar.prcal(year)

Output

If you run the above program, you will get the following results.

calendar.weekday(year, month, day)

The method calendar.weekday(year, month, day) takes three arguments and returns the weekday number.

Example

 Live Demo

# importing the calendar module
import calendar
# initializing year, month and day
year = 2020
month = 1
day = 28
# getting weekday
print(calendar.weekday(year, month, day))

Output

If you run the above code, you will get the following results.

1

Conclusion

If you have any doubts in the tutorial, mention them in the comment section.

raja
Published on 02-Jan-2020 06:45:07
Advertisements