Certainly! The calendar
module in Python provides useful functions and classes related to calendars. Here’s a simple tutorial with examples:
1. Displaying a Calendar for a Given Month:
import calendar
# Get the calendar for a specific month
year = 2024
month = 1 # January
# Display the calendar
cal = calendar.month(year, month)
print(f"Calendar for {calendar.month_name[month]} {year}:\n{cal}")
2. Displaying a Calendar for a Given Year:
import calendar
# Get the calendar for a specific year
year = 2024
# Display the calendar
cal = calendar.calendar(year)
print(f"Calendar for the year {year}:\n{cal}")
3. Determining if a Year is a Leap Year:
import calendar
# Check if a year is a leap year
year = 2024
if calendar.isleap(year):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
4. Finding the First Weekday of the Month:
import calendar
# Get the first weekday of a month
year = 2024
month = 1 # January
first_weekday = calendar.monthrange(year, month)[0]
print(f"The first weekday of {calendar.month_name[month]} {year} is {calendar.day_name[first_weekday]}.")
5. Displaying Calendar with Custom Formatting:
import calendar
# Create a TextCalendar object with different formatting
cal = calendar.TextCalendar(calendar.SUNDAY) # Start week with Sunday
# Display the calendar for a specific month
year = 2024
month = 1 # January
cal_text = cal.formatmonth(year, month)
print(f"Calendar for {calendar.month_name[month]} {year}:\n{cal_text}")
These examples cover some basic functionalities of the calendar
module in Python. You can explore more features and options available in the calendar
module based on your specific needs.