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.
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.
# importing the calendar module import calendar # initializing the year year = 2019 # printing the calendar print(calendar.calendar(2019))
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().
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.
# 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))
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
If you are having any trouble following the tutorial, comment below.