Python program to display calendar
In this post, you will learn how to write a Python program to display Calendar.
Python provides a built-in function calendar to work with date-related tasks. Calendar module in Python has the calendar class that permits the calculations for various task based on date, month, and year and also provides additional useful functions related to the calendar.
Python print a year calendar
For printing a year calendar, first we need to import the module and initialize the year number. The built-in function calendar() inside the module takes in the year and displays the calendar for that year. Then, we use the calendar.calendar(year) class to print the year calendar.
# importing the calendar module
import calendar
# initializing the year
year = 2021
# printing the calendar
print(calendar.calendar(year))
Output of the above program: Python print a month calendar
In the given example, we are taking year and month inputs from the user and printing the month calendar. The built-in function month() of the module takes in the year and the month and displays the calendar for that month of the year.
# importing the calendar module
import calendar
# Getting month and year input from the user
yy = int(input("Enter year: "))
mm = int(input("Enter month: "))
# display the calendar
print(calendar.month(yy, mm))
Output of the above code:
Enter year: 2021
Enter month: 3
March 2021
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
Related Articles
Python convert dataframe to numpy array
Convert binary to decimal in Python
Multiply all elements in list Python
Multiply each element of a list by a number in Python
Python NumPy: Overview and Examples
Convert Python list to numpy array
numpy dot product
Python Pandas Plotting
Pandas string to datetime
Convert Excel to CSV Python Pandas
Python take screenshot of specific window
Read data from excel file using Python Pandas
Quick Introduction to Python Pandas
Python requests GET method
Python Convert XML to CSV
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Remove character from string Python
Python raise keyword