Python has a built-in module called calendar to work with calendars. We are going to learn about the calendar module in this article.
The week in the calendar modules starts on Monday and ends on Sunday. The module calendar follows the Gregorian calendar. Let's see some useful methods of the calendar module.
Getting Year Calendar
If you have to get the calendar of the specific year, then create the instance of the class calendar.calendar(year) and print it. Let's see one example.
Example
# importing the calendar module import calendar # initializing the year year = 2019 # printing the calendar print(calendar.calendar(2019))
Output
If you run the above code, you will get the following results.
We can get different types of outputs using the calendar.calendar(year) instance. Try to learn those methods using the dir().
Getting Month Calendar
If you have to get the calendar of the specific month, then use the method calendar.month(year, month_number) and print it. Let's see one example.
Example
# importing the calendar module import calendar # initializing the yearn and month number year = 2000 month = 1 # getting the calendar of the month print(calendar.month(year, month))
Output
If you run the above program, you will get the following results.
January 2000 Mo Tu We Th Fr Sa Su 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
Conclusion
If you are having any trouble following the tutorial, comment below.