Display Calendar in Python

In this Python lesson, we will practice how to write a program to display the monthly calendar of a year. Python has a built-in function, a calendar to work with date-related tasks.

Example: Display Monthly Calendar

# import calendar module
import calendar

yr = int(input('Enter year: '))
mth = int(input('Enter month: '))

# display the calendar
print(calendar.month(yr, mth))

Output

Enter year: 2030
Enter month: 1

January 2030
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

In the above program, we import the calendar module from the Python library. The calendar.month() built-in function inside the module automatically computes the year and the month and displays the calendar for that month of the year on the console once the user inserts the year and the month when the program prompts.

Do you want to become an expert programmer? See books helping our students, interns, and beginners in software companies today (Click here)!

Leave a Comment

Python Tutorials