Python Calendar monthcalendar Function

The monthcalendar function in Python's calendar module returns a matrix representing a month’s calendar. Each row of the matrix represents a week, and each day is represented by an integer. Days outside the month are represented by zeros. This function is useful for generating a structured representation of a month's calendar.

Table of Contents

  1. Introduction
  2. monthcalendar Function Syntax
  3. Examples
    • Basic Usage
    • Generating a Month Calendar
  4. Real-World Use Case
  5. Conclusion

Introduction

The monthcalendar function in Python's calendar module generates a matrix (a list of lists) representing the calendar for a specific month and year. Each week is represented by a list of integers, where each integer corresponds to a day in the month, and zeros represent days outside the specified month.

monthcalendar Function Syntax

Here is how you use the monthcalendar function:

import calendar

month_calendar = calendar.monthcalendar(year, month)

Parameters:

  • year: The year as an integer.
  • month: The month as an integer (1-12).

Returns:

  • A matrix (list of lists) representing the month’s calendar. Each row represents a week, and each day is represented by an integer. Days outside the month are represented by zeros.

Examples

Basic Usage

Here is an example of how to use the monthcalendar function to generate a calendar for a specific month and year.

Example

import calendar

# Generating a calendar for July 2024
year = 2024
month = 7
month_calendar = calendar.monthcalendar(year, month)
print(month_calendar)

Output:

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

Generating a Month Calendar

This example demonstrates how to generate and print a month calendar in a readable format.

Example

import calendar

def print_month_calendar(year, month):
    month_calendar = calendar.monthcalendar(year, month)
    print(f"{calendar.month_name[month]} {year}")
    print("Mo Tu We Th Fr Sa Su")
    for week in month_calendar:
        print(" ".join(f"{day:2}" if day != 0 else "  " for day in week))

# Example usage
print_month_calendar(2024, 7)

Output:

July 2024
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            

Real-World Use Case

Highlighting Specific Dates

In real-world applications, the monthcalendar function can be used to generate a calendar and highlight specific dates, such as holidays or events.

Example

import calendar

def highlight_dates(year, month, dates_to_highlight):
    month_calendar = calendar.monthcalendar(year, month)
    print(f"{calendar.month_name[month]} {year}")
    print("Mo Tu We Th Fr Sa Su")
    for week in month_calendar:
        for day in week:
            if day == 0:
                print("  ", end=" ")
            elif day in dates_to_highlight:
                print(f"[{day:2}]", end=" ")
            else:
                print(f" {day:2} ", end=" ")
        print()

# Example usage
highlight_dates(2024, 7, [4, 14, 20])

Output:

July 2024
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              

Conclusion

The monthcalendar function in Python's calendar module generates a matrix representing a month’s calendar, where each row corresponds to a week and each day is represented by an integer. This function is useful for generating structured representations of calendars, which can be easily formatted and manipulated for various applications.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare