Write a program of leap year in python - Leap Year program in python - Python Programs - Pattern programs in python

 Leap Year Program in Python



A leap year is a year that is divisible by 4, except for the years divisible by 100 but not by 400. Here's a Python program to check if a given year is a leap year:

Python Code:

def is_leap_year(year):

    """

    Returns True if the given year is a leap year, False otherwise.

    """

    if year % 4 == 0:

        if year % 100 == 0:

            if year % 400 == 0:

                return True

            else:

                return False

        else:

            return True

    else:

        return False

# example usage

print(is_leap_year(2020))  # True

print(is_leap_year(2021))  # False

print(is_leap_year(2000))  # True

print(is_leap_year(1900))  # False

Here is the Practical of the above program in Jupyter Notebook



The is_leap_year function takes a year as input and returns True if the year is a leap year, and False otherwise. The function first checks if the year is divisible by 4. If it is not, the function returns False. If the year is divisible by 4, the function checks if it is divisible by 100. If it is, the function checks if it is divisible by 400. If it is, the function returns True, otherwise, it returns False.

Note that this program assumes the Gregorian calendar, which is the most widely used civil calendar in the world. If you need to work with a different calendar system, you may need to modify the program accordingly.

Comments