Skip to content
Related Articles

Related Articles

Python calendar module | monthdayscalendar() method
  • Last Updated : 22 Oct, 2018

Calendar module allows to output calendars like program, and provides additional useful functions related to the calendar. Functions and classes defined in Calendar module use an idealized calendar, the current Gregorian calendar extended indefinitely in both directions.

monthdayscalendar() method in Python is used to get a list of the weeks in the month of the year as full weeks.

Syntax: monthdayscalendar(year, month)

Parameter: 
year: year of the calendar
month: month of the calendar

Returns: a list of the weeks in the month.

Code #1:




# Python program to demonstrate working
# of monthdayscalendar() method
  
# importing calendar module
import calendar
  
obj = calendar.Calendar()
  
year = 2018
mnth = 9
  
# priting with monthdayscalendar
print(obj.monthdayscalendar(year, mnth))


Output:

[[0, 0, 0, 0, 0, 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]]



Note that weeks in the output are lists of seven day numbers.
 
Code #2: iterating the list of weeks




# Python program to demonstrate working 
# of monthdayscalendar() method
  
# importing calendar module
import calendar
  
obj = calendar.Calendar()
  
# iteratign with monthdayscalendar
for day in obj.monthdayscalendar(2018, 9):
    print(day)


Output:

[0, 0, 0, 0, 0, 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]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :