×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python calendar monthcalendar() Method with Example

Python calendar.monthcalendar() Method: In this tutorial, we will learn about the monthcalendar() method of calendar module in Python with its usage, syntax, and examples. By Hritika Rajput Last updated : April 24, 2023

Python calendar.monthcalendar() Method

The calendar.monthcalendar() method is an inbuilt method of the calendar module, it 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

The following module is required to use monthcalendar() method:

import calendar

Syntax

The following is the syntax of monthcalendar() method:

monthcalendar(year, month)

Parameter(s)

The following are the 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 of calendar.monthcalendar() Method in Python

# 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!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.