Python Program to Display Calendar

This article is created to cover some programs in Python, to display calendar of a month or all months (whole year). Here are the list of programs:

Display Calendar of a Month

To display calendar in Python, you have to ask from user to enter the Year and a month. The month must be entered as its number. For example, to display calendar of January, 2021, then enter 2021 as year input and 1 as month input like shown in the program and its output given below:

import calendar

print("Enter Year: ")
yy = input()
print("\nEnter Month Number (1-12): ")
mm = input()

y = int(yy)
m = int(mm)
print("\n", calendar.month(y, m))

Here is the initial output produced by this Python program:

calendar program in python

Now supply the input say 2021 as year, press ENTER key and then type 1 as month, again press ENTER key to display the calendar of January, 2021 as shown in the snapshot given below:

calendar python

Note - The calendar module in Python allows us to work with calendar. That is, calendar.month() prints calendar of a month. For example, calendar.month(2021, 4) prints calendar of fourth month of year 2021.

Modified Version of Previous Program

This is the modified version of previous program. The end= used in this program to skip inserting an automatic newline using print(). This program also uses try-except to handle with invalid input.

import calendar

print("Enter Year: ", end="")
try:
    yy = int(input())
    print("\nEnter Month Number (1-12): ", end="")
    try:
        mm = int(input())
        if mm>=1 and mm<=12:
            print("\n", calendar.month(yy, mm))
        else:
            print("\nInvalid Month Number!")
    except ValueError:
        print("\nInvalid Input!")
except ValueError:
    print("\nInvalid Input!")

Here is its sample run with user input, 2021 as year and 2 as month number:

python display calendar

In above program, the following code:

if mm>=1 and mm<=12:

is used to check whether the value of month number entered by user is a valid input or not. That is, if user enters month number from 1 to 12, then it is a valid month number, otherwise will treated as an invalid input.

Display All Months (Whole Year) Calendar

Now this program receives an input as year, and prints calendar of all months of given year. For example, if user enters 2022 as year, then whole year's calendar gets printed like shown in the program and its output given below:

import calendar

print("Enter Year: ", end="")
try:
    yy = int(input())
    print()
    mm = 1
    while mm<=12:
        print(calendar.month(yy, mm))
        mm = mm+1
except ValueError:
    print("\nInvalid Input!")

Here is its sample run with year input 2021:

python display whole year calendar

The snapshot given above is upper part of complete output. Here is the lower part of complete output produced by above program with exactly same user input, that is 2021:

display all months calendar of given year python

The dry run of following block of code (from above program):

mm = 1
while mm<=12:
    print(calendar.month(yy, mm))
    mm = mm+1

goes like:

Python Online Test


« Previous Program Next Program »