Write a Python program display a list of the dates for the 2nd Friday of every month for a given year


The program is used to print the date of the second Friday of every month for the year 2022 using the calendar module in Python.

  • The program imports the calendar module.
  • Then it iterates over each month of the year using the range function for months 1 to 12.
  • For each month, it calls the monthcalendar() method with the year and month as arguments, which returns a list of lists representing the calendar for that month.
  • It then gets the first, second, and third weeks of the month from the list returned by monthcalendar().
  • If the first Friday of the month is present in the first week of the month, it selects the second Friday of the month from the second week of the month. Otherwise, it selects the second Friday of the month from the third week of the month.
  • Finally, it prints the month abbreviation and the date of the second Friday of that month using the month_abbr method of the calendar module.

Source Code

import calendar
for month in range(1, 13):
    cal = calendar.monthcalendar(2022, month)
    first_week  = cal[0]
    second_week = cal[1]
    third_week  = cal[2]
 
    if first_week[calendar.FRIDAY]:
        holi_day = second_week[calendar.FRIDAY]
    else:
        holi_day = third_week[calendar.FRIDAY]
 
    print("%s : %s" % (calendar.month_abbr[month], holi_day))

Output

Jan : 14
Feb : 11
Mar : 11
Apr : 8
May : 13
Jun : 10
Jul : 8
Aug : 12
Sep : 9
Oct : 14
Nov : 11
Dec : 9

Example Programs