Python program to check leap year



What is a leap year?

An extra day is added to the calendar almost every four years as of February 29, and the day is called a leap day. It corrects the calendar for the fact that our planet takes approximately 365.25 days to orbit the sun. A leap year contains a leap day.

In the Gregorian calendar, two conditions are used to identify leap years:

  • The year that can be evenly divided by 4 but not by 100, is a leap year.
  • The year is also a leap year if it is evenly divisible by 400.

This means that in the Gregorian calendar, the years such as 2000, 2004, 2008, 2012, 2016, 2020, and 2024 are leap years, while 2001, 2002, 2003, 2018, 2019, and 2021 are not leap years.

You will get access to two programs to check whether the year is a leap year or not. The first program is using conditional statements only and the second program is using conditional statements and the function.

Source code 1 (Using conditional statements only):

year = int(input("Enter a year:"))

if year % 4 == 0 and year % 100 != 0:
    print(f"{year} is a leap year.")
elif year % 400 == 0:
    print(f"{year} is a leap year.")
elif year % 100 == 0:
    print(f"{year} is not a leap year.")
else:
    print(f"{year} is not a leap year.")

Output:

>>>
Enter a year:2020
2020 is a leap year.

>>> 
Enter a year:2016
2016 is a leap year.

>>> 
Enter a year:2018
2018 is not a leap year.

>>> 
Enter a year:2021
2021 is not a leap year.


Source code 2 (Using conditional statements and the function):

def is_leap(year):
    if year % 4 == 0 and year % 100 != 0:
        print(f"{year} is a leap year.")
    elif year % 400 == 0:
        print(f"{year} is a leap year.")
    elif year % 100 == 0:
        print(f"{year} is not a leap year.")
    else:
        print(f"{year} is not a leap year.")

year = int(input("Enter a year:"))

is_leap(year)

Output:

>>>
Enter a year:2020
2020 is a leap year.

>>> 
Enter a year:2016
2016 is a leap year.

>>> 
Enter a year:2018
2018 is not a leap year.

>>> 
Enter a year:2021
2021 is not a leap year.