Python – Last business day of every month in year

03 Aug, 2021
Given a year and weekday number, the task is to write a Python program to extract each date of the month corresponding to the weekday.
Input : year = 1997, weekdy = 5
Output : [’25/1/1997′, ’22/2/1997′, ’29/3/1997′, ’26/4/1997′, ’31/5/1997′, ’28/6/1997′, ’26/7/1997′, ’30/8/1997′, ’27/9/1997′, ’25/10/1997′, ’29/11/1997′, ’27/12/1997′]
Explanation : 5 is friday. Last weekday of january 1997 which is friday is 25.
Input : year = 1996, weekdy = 4
Output : [’26/1/1996′, ’23/2/1996′, ’29/3/1996′, ’26/4/1996′, ’31/5/1996′, ’28/6/1996′, ’26/7/1996′, ’30/8/1996′, ’27/9/1996′, ’25/10/1996′, ’29/11/1996′, ’27/12/1996′]
Explanation : 4 is thursday. Last weekday of january 1997 which is thursday is 26.
Method #1 : Using loop + max() + calendar.monthcalendar
In this, we perform the task of getting each month calendar using monthcalendar() from the calendar library. Each weekday date is extracted and the maximum of it, being the maximum of the whole month is the last weekday, hence extracted.
Python3
# Python3 code to demonstrate working of# Last weekday of every month in year# Using loop + max() + calendar.monthcalendarimport calendar  # initializing yearyear = 1997  # printing Yearprint("The original year : " + str(year))  # initializing weekday weekdy = 5  # iterating for all monthsres = []for month in range(1, 13):          # max gets last friday of each month of 1997    res.append(str(max(week[weekdy]                        for week in calendar.monthcalendar(year, month))) +               "/" + str(month)+ "/" + str(year))  # printing print("Last weekdays of year : " + str(res))
Output:
The original year : 1997
Last weekdays of year : [’25/1/1997′, ’22/2/1997′, ’29/3/1997′, ’26/4/1997′, ’31/5/1997′, ’28/6/1997′, ’26/7/1997′, ’30/8/1997′, ’27/9/1997′, ’25/10/1997′, ’29/11/1997′, ’27/12/1997′]
Method #2: Using list comprehension
Similar to the above method, the only difference being the usage of list comprehension for a compact solution.
Python3
# Python3 code to demonstrate working of# Last weekday of every month in year# Using list comprehensionimport calendar  # initializing yearyear = 1997  # printing Yearprint("The original year : " + str(year))  # initializing weekday weekdy = 5  # list comprehension for shorthandres = [str(max(week[weekdy]               for week in calendar.monthcalendar(year, month))) +        "/" + str(month)+ "/" + str(year) for month in range(1, 13)]  # printing print("Last weekdays of year : " + str(res))
Output:
The original year : 1997
Last weekdays of year : [’25/1/1997′, ’22/2/1997′, ’29/3/1997′, ’26/4/1997′, ’31/5/1997′, ’28/6/1997′, ’26/7/1997′, ’30/8/1997′, ’27/9/1997′, ’25/10/1997′, ’29/11/1997′, ’27/12/1997′]
Python datetime-program
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python | Convert string dictionary to dictionary
Python Program for Fibonacci numbers