Python Program To Find Number Of Days In A Given Month And Year

Chapter: Python Last Updated: 22-09-2023 04:57:25 UTC

Program:

            /* ............... START ............... */
                

import calendar

def find_number_of_days(year, month):
    # Use the monthrange function from the calendar module
    _, num_days = calendar.monthrange(year, month)
    return num_days

# Input year and month from the user
year = int(input("Enter a year: "))
month = int(input("Enter a month (1-12): "))

# Check if the input month is valid
if 1 <= month <= 12:
    num_days = find_number_of_days(year, month)
    print(f"Number of days in {calendar.month_name[month]}, {year}: {num_days}")
else:
    print("Invalid month input. Please enter a month between 1 and 12.")

                /* ............... END ............... */
        

Output

Enter a year: 2023
Enter a month (1-12): 2
Number of days in February, 2023: 28


In this example, the user entered the year 2023 and the month 2 (which corresponds to 
February), and the program correctly determined that February 2023 has 28 days.

Notes:

  • This program first imports the calendar module, then defines a function find_number_of_days that takes a year and a month as input and uses calendar.monthrange to determine the number of days in that month. It then takes user input for the year and month and prints the result.

Tags

Python program to find number of days in a given month and year #How to get the number of days in a month and year in Python? # Number of days in a given month of a year in python

Similar Programs Chapter Last Updated
Python Program To Check Whether Element Present In Set Or Not Example Python 04-10-2023
Python Program To Find Maximum And Minimum Number In A Set Python 04-10-2023
Python Program To Check Symmetric Matrix Python 04-10-2023
Python Program To Find Subsets Of A Set Python 04-10-2023
Python Program To Find Power Set Of A Set Python 04-10-2023
Remove All Duplicates From List Python Python 04-10-2023
Python Program To Find Symmetric Difference Of Two Sets Python 27-09-2023
Python Program To Find Common Item From Two Set Python 27-09-2023
Python Program To Get Unique Values From A List Python 27-09-2023
Python Encode And Decode String With Key Python 24-09-2023
Python Simple Encrypt Decrypt String Python 24-09-2023
Python Format String To Specific Length Python 24-09-2023
Python Code To Check If String Contains Substring Python 24-09-2023
Python Program To Find Most Repeated Word In A String Python 23-09-2023
Split String Into Words Python Python 23-09-2023
Remove All Punctuation Python Python 23-09-2023
Python Program To Reverse An Array Python 23-09-2023
Python Program To Find Number Of Palindrome In A String Python 23-09-2023
Python Program To Find Longest Common Substring Python 23-09-2023
Python Program To Calculate Age Of A Person Python 22-09-2023
Python Code To Get Day Of Week Python 22-09-2023
Python Convert String To Date Without Time Python 22-09-2023
Python Program To Print Current Date And Time In Format dd/mm/yyyy Python 22-09-2023
Python Program To Find Working Days In A Month Python 19-09-2023
Python Code To Change Date Format Python 16-09-2023
Python Program To Calculate Number Of Days Between Two Dates Python 16-09-2023
Python Program To Calculate Age In Years Months And Days Python 16-09-2023
Python Program To Schedule A Job To Run After A Certain Amount Of Time Python 10-08-2023
Python Program To Schedule A Job To Run Randomly Once A Day Python 10-08-2023
Python Program To Schedule A Job To Run Every Hour Python 10-08-2023

1 2 3 4