Write a Python Program to Display Calendar
Here’s a Python program that displays a calendar for a given year and month using the calendar module:
import calendar
year = int(input("Enter year: "))
month = int(input("Enter month: "))
cal = calendar.month(year, month)
print(cal)
Explanation:
- The program starts by importing the calendar module, which provides functions to work with calendars and dates.
- The user is prompted to enter the year and month for which they want to display the calendar.
- The calendar.month() function takes the year and month as arguments and returns a string containing the formatted calendar for the specified year and month.
- The print() function is used to display the generated calendar on the screen.
Output:
Enter year: 2023
Enter month: 8
August 2023
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 this example, if the user enters the year 2023 and the month 8, the program uses the calendar module to generate and display the calendar for August 2023. The calendar includes the days of the week (Mo, Tu, etc.) and the corresponding days of the month.