Write a Python program to get the last day of a specified year and month


The program starts by importing the calendar module from the Python Standard Library, which provides functions for working with calendars. Two variables, year and month, are created and set to the values 1975 and 2, respectively.

The calendar.monthrange function is called with the year and month as arguments, which returns a tuple with two values: the first day of the week of the first day of the month (where Monday is 0 and Sunday is 6), and the number of days in the month. The second value of the tuple is retrieved using indexing [1], and is stored in the variable days_in_month. Finally, the result of the calculation is printed using the print function.

Source Code

import calendar
year = 1975
month = 2
print(calendar.monthrange(year, month)[1])

Output

28

Example Programs