{"id":25970,"date":"2021-12-03T21:29:41","date_gmt":"2021-12-03T15:59:41","guid":{"rendered":"https:\/\/python-programs.com\/?p=25970"},"modified":"2021-12-03T21:38:36","modified_gmt":"2021-12-03T16:08:36","slug":"python-program-for-calendar-monthdayscalendar-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-calendar-monthdayscalendar-method-with-examples\/","title":{"rendered":"Python Program for calendar monthdayscalendar() Method with Examples"},"content":{"rendered":"
Calendar Module:<\/strong><\/p>\n 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.<\/p>\n monthdayscalendar() Method:<\/strong><\/p>\n The monthdayscalendar() method returns a list of full weeks in the specified month of the year. Weeks are a list of seven-day numbers.<\/p>\n Syntax:<\/strong><\/p>\n Parameter Values:<\/strong><\/p>\n year:<\/strong>\u00a0This is required. It is a number. The year for which the calendar should be created.<\/p>\n month:\u00a0<\/strong>This is required. It is a number. The month for which the calendar should be created.<\/p>\n Return Value: <\/strong>This function returns a list of weeks in the month.<\/p>\n Example1: Using For Loop<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Note:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example1: Using For Loop<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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. monthdayscalendar() Method: The monthdayscalendar() method returns a list of full weeks in the specified month of the …<\/p>\nmonthdayscalendar(year, month)<\/pre>\n
Program for calendar monthdayscalendar() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import calendar module using the import keyword.\r\nimport calendar\r\n# Give the year as static input and store it in a variable.\r\ngvn_yr = 2020\r\n# Give the month as static input and store it in another variable.\r\ngvn_mont = 4\r\n# Call the Calendar() function and store it in another variable.\r\ncalendr = calendar.Calendar()\r\n# Apply monthdayscalendar() method to the above calendar by passing the given year,\r\n# month as the arguments and store it in another variable.\r\nrslt = calendr.monthdayscalendar(gvn_yr, gvn_mont)\r\n# Iterate in the above result using the for loop.\r\nfor itr in rslt:\r\n # Inside the loop, print the iterator value.\r\n print(itr)<\/pre>\n
[0, 0, 1, 2, 3, 4, 5]\r\n[6, 7, 8, 9, 10, 11, 12]\r\n[13, 14, 15, 16, 17, 18, 19]\r\n[20, 21, 22, 23, 24, 25, 26]\r\n[27, 28, 29, 30, 0, 0, 0]<\/pre>\n
It's worth noting that the weeks in the output are simply lists of seven-day\r\nnumbers.<\/pre>\n
\n
# Import calendar module using the import keyword.\r\nimport calendar\r\n# Give the year as static input and store it in a variable.\r\ngvn_yr = 2012\r\n# Give the month as static input and store it in another variable.\r\ngvn_mont = 6\r\n# Call the Calendar() function and store it in another variable.\r\ncalendr = calendar.Calendar()\r\n# Apply monthdayscalendar() method to the above calendar by passing the given year,\r\n# month as the arguments and store it in another variable.\r\nrslt = calendr.monthdayscalendar(gvn_yr, gvn_mont)\r\n# Print the above result.\r\nprint(rslt)\r\n<\/pre>\n
[[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, 0]]<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import calendar module using the import keyword.\r\nimport calendar\r\n# Give the year as user input using the int(input()) function and store it in a variable.\r\ngvn_yr = int(input(\"Enter some random year = \"))\r\n# Give the month as user input using the int(input()) function and store it in another variable.\r\ngvn_mont = int(input(\"Enter some random month = \"))\r\n# Call the Calendar() function and store it in another variable.\r\ncalendr = calendar.Calendar()\r\n# Apply monthdayscalendar() method to the above calendar by passing the given year,\r\n# month as the arguments and store it in another variable.\r\nrslt = calendr.monthdayscalendar(gvn_yr, gvn_mont)\r\n# Iterate in the above result using the for loop.\r\nfor itr in rslt:\r\n # Inside the loop, print the iterator value.\r\n print(itr)<\/pre>\n
Enter some random year = 2013\r\nEnter some random month = 2\r\n[0, 0, 0, 0, 1, 2, 3]\r\n[4, 5, 6, 7, 8, 9, 10]\r\n[11, 12, 13, 14, 15, 16, 17]\r\n[18, 19, 20, 21, 22, 23, 24]\r\n[25, 26, 27, 28, 0, 0, 0]<\/pre>\n","protected":false},"excerpt":{"rendered":"