Write a Python program to print a calendar for month
The Python program uses the built-in calendar module to display a calendar for a given month and year entered by the user. Here is a step-by-step explanation of the program:
- import calendar: This line imports the built-in calendar module, which provides functions for working with calendars, including displaying calendars for a given year or month.
- y = int(input("Enter the Year :")) : This line prompts the user to enter a year by displaying the message "Enter the Year :" on the screen. The input() function reads the input entered by the user as a string, which is then converted to an integer using the int() function and assigned to the variable y.
- m = int(input("Enter the Month of Number :")) : This line prompts the user to enter a month number by displaying the message "Enter the Month of Number :" on the screen. The input() function reads the input entered by the user as a string, which is then converted to an integer using the int() function and assigned to the variable m.
- print(calendar.month(y,m)) : This line calls the month() function of the calendar module to display a calendar for the month m and year y entered by the user. The month() function takes two arguments: the year and the month number. It returns a string that represents the calendar for the given month and year. This string is then printed on the console using the print() function.
The output of the program will be a calendar for the month and year entered by the user, with each day displayed in a separate block. The calendar will show the dates for the given month, along with the weekdays and weekends highlighted in different colors or styles.
Source Code
import calendar
y = int(input("Enter the Year :"))
m = int(input("Enter the Month of Number :"))
print(calendar.month(y,m))
Output
Enter the Year :2023
Enter the Month of Number :10
October 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