Python Program to Check Leap Year or Not

Python Program to Check Leap or Not

In this tutorial, we are going to write a python datetime leap year program to check leap year.

A leap year is one which has 366 days in a year. A year is a leap year if it is divisible by 4 unless it is a century that is not divisible by 100. If the year is divisible by 4 then it is a leap year or if year is not divisible by 100 and at the same time divisible by 400 then also it is a leap year, otherwise it is not a leap year.

Steps on How to find/Check a year is Leap or not in python?

  • Year is leap if is divisible by 4 i.e year % 4 == 0.
  • Given year is leap if it divisible by 400 and not divisible by 100 i.e. year % 400 == 0 and year % 100!=0.
  • Otherwise given year is not leap year.

Program/Source Code

# Python program to check leap year or not 

def checkLeapYear(year): 
    if (year % 4 == 0 or (year % 100!= 0 and year % 400 ==0)):
        return True
    else:
        return False

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

if(checkLeapYear(year)): 
    print(year, "is Leap Year") 
else: 
    print(year, "is not a Leap Year")

Output

Enter year: 1992
1992 is a Leap Year

Enter year: 2020
2020 is Leap Year

Enter year: 4667
4667 is not a Leap Year

Explanation:

Accept the year as input from user. We have written a function checkLeapYear(). Using if-else to check given year is leap or not by considering above conditions. Return True if any one condition satisfies otherwise false.

Program to find next 15 leap years in python

Program/Source Code


def find_leap_years(given_year):
    count=1
    list_of_leap_years=[]
    
    while(count<=15):
        if(given_year%4==0 or given_year%400==0 and given_year%100==0):
            list_of_leap_years.append(given_year)
            count=count+1
        given_year=given_year+1
    return list_of_leap_years

list_of_leap_years=find_leap_years(1992)
print(list_of_leap_years)

Output

[1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, 2044, 2048]

Comments