ADVERTISEMENT
ADVERTISEMENT

Python calendar Module | monthcalendar() Method with Example

Python calendar.monthcalendar() Method: Here, we are going to learn about the monthcalendar() method of calendar module in Python with its definition, syntax, and examples.
Submitted by Hritika Rajput, on May 08, 2020

Python calendar.monthcalendar() Method

monthcalendar() method is an inbuilt method of the calendar module in Python. It works on simple text calendars and returns a matrix representing the given month's calendar. Each row in the matrix represents a full week, and the days outside of the month are represented by zeros.

Module:

    import calendar

Syntax:

    monthcalendar(year, month)

Parameter(s):

  • year: It is a required parameter, which represents the year of the calendar.
  • month: It is a required parameter, which represents the month of the calendar.

Return value:

The return type of this method is <class 'list'> (a matrix), which represents the calendar of the given month of the given year.

Example:

# Python program to illustrate the 
# use of monthcalendar() method

# importing calendar module
import calendar

year = 2020
month = 4

x = calendar.monthcalendar(year, month)
print("Calendar of April 2020 as a matrix")
for i in range(len(x)):
        print(x[i])
print()

# Prints the calendar of the given month 
# where 0 values are days out of that month

year = 1997
month = 1
a, b= calendar.monthrange(year, month)
x = calendar.monthcalendar(year, month)
print("Weekday starts from:", a)
print("Calendar of January 1997 as a matrix")
for i in range(len(x)):
    print(x[i])
print()
# You can see the month starts from x[0][a]

year = 2000
month = 5
x = calendar.monthcalendar(year, month)
print("calendar of May 2020 as a matrix")
for i in range(len(x)):
    print(x[i])
print()


calendar.setfirstweekday(6)
x = calendar.monthcalendar(year, month)
# Here column number 0  represents Sunday and so on
print("Calendar of May 2020 with first column representing Sunday")
for i in range(len(x)):
    print(x[i])  

Output

Calendar of April 2020 as a matrix
[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, 0, 0]

Weekday starts from: 2
Calendar of January 1997 as a matrix
[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, 31, 0, 0]

calendar of May 2020 as a matrix
[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, 0, 0, 0, 0]

Calendar of May 2020 with first column representing Sunday
[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, 31, 0, 0, 0]
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.