Python Calendar Program (Code to Print Calendar of Any Year)
In programming, calendars play an essential role in managing dates, scheduling events, and visualizing time-based data. Being able to display or print a calendar in Python is a valuable skill that allows us to create calendar applications, generate reports, and perform date-related operations efficiently.
In this tutorial, we will explore the Python calendar program that enables us to print or display a calendar. We will provide a step-by-step guide, complete with examples and code explanations, to help you understand the concept and implement it in your Python programs effectively.
The Gregorian calendar program in Python involves generating a visual representation of the days, weeks, and months for a specific year or month. It allows us to visualize the structure of time and enables users to interact with dates and events in a meaningful way.
The importance of understanding how to display a calendar in Python extends beyond basic date management. It finds applications in various domains, such as appointment scheduling, project planning, event tracking, and data analysis.
For beginners interested in learning this programming language, it is recommended to start with the free Python Tutorial.
To practice the programs on your own, you can use the online Python compiler, which allows you to write, run, and test Python code directly in your web browser. It provides a convenient way to experiment with code and see the results immediately.
Calendar Program in Python
Here's an example of Python program to display calendar that uses the calendar module:
Code
import calendar
# Taking input from the user
year = int(input("Enter the year: "))
month = int(input("Enter the month number: "))
# Displaying the calendar
print(calendar.month(year, month))
Output
Enter the year: 2023
Enter the month number: 6
June 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
Explanation
-
We start by importing the calendar module, which provides functions to work with calendars.
-
We take input from the user for the year and month using input(), and convert them to integers using int().
-
To display the calendar, we use the month() function from the calendar module. It takes the year and month as arguments and returns a formatted calendar for that specific month and year.
-
Finally, we print the formatted calendar using print().
This program allows users to input the desired year and month, and it will display the corresponding calendar. The calendar module handles the complexities of calendar calculations, making it easy to generate accurate and properly formatted calendars.
Calendar Program in Python (Without Module)
Here's an example of a calendar program in Python without module:
Code
def is_leap_year(year):
# Check if the year is a leap year
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
else:
return True
else:
return False
def get_month_days(year, month):
# Get the number of days in a given month
if month in [1, 3, 5, 7, 8, 10, 12]:
return 31
elif month == 2:
if is_leap_year(year):
return 29
else:
return 28
else:
return 30
def print_calendar(year, month):
# Print the calendar for a given year and month
month_names = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December']
# Print the month and year
print(month_names[month-1], year)
# Print the days of the week
print('Sun Mon Tue Wed Thu Fri Sat')
# Get the starting day of the month
starting_day = sum([get_month_days(year, m) for m in range(1, month)]) % 7
# Print the empty spaces for the starting day
print(' ' * starting_day, end='')
# Print the days of the month
for day in range(1, get_month_days(year, month) + 1):
print(f'{day:2d} ', end='')
# Move to the next line after Saturday
if (day + starting_day) % 7 == 0:
print()
# Move to the next line after printing the calendar
print()
# Taking input from the user
year = int(input("Enter the year: "))
month = int(input("Enter the month number: "))
# Displaying the calendar
print_calendar(year, month)
Output
Enter the year: 2023
Enter the month number: 7
July 2023
Sun Mon Tue Wed Thu Fri Sat
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
Explanation
-
The program defines three functions: is_leap_year(), get_month_days(), and print_calendar().
-
is_leap_year(year) checks if a given year is a leap year by following the rules for leap year calculations.
-
get_month_days(year, month) returns the number of days in a given month, taking into account leap years.
-
print_calendar(year, month) prints the calendar for the specified year and month.
-
The program takes user input for the year and month.
-
Finally, it calls the print_calendar() function to display the calendar.
This program calculates the calendar by determining the starting day of the month and printing the days in a grid format. It does not rely on any external modules, allowing you to understand the logic behind calendar generation.
Python Program to Print Current Month Calendar
Here's a Python program that prints the calendar for the current month:
Code
import calendar
# Get the current year and month
current_year = calendar.datetime.datetime.now().year
current_month = calendar.datetime.datetime.now().month
# Print the calendar for the current month
print(calendar.month(current_year, current_month))
Output
June 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
Explanation
-
The program imports the calendar module, which provides functions to work with calendars.
-
It uses the datetime module from the calendar module to get the current year and month.
-
The datetime.datetime.now().year statement returns the current year, and datetime.datetime.now().month returns the current month.
-
Finally, the program uses the month() function from the calendar module to print the calendar for the current month.
When you run this program, it will automatically display the calendar for the current month. The calendar module handles the complexities of calendar calculations, making it easy to generate accurate and properly formatted calendars.