Leap Year Program in Python

In this post, we will write a Leap year Program in Python. Based on the given input it will check the given year is a leap year or not?

A year is called leap year if the year is divisible by four, except for the years which are divisible by 100 but not divisible by 400. Therefore, the year 2000 was a leap year, but the years 1700, 1800, and 1900 were not.

The complete list of leap years in the first half of the 21st century is 2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, 2044, and 2048.

Python Program to Check Leap year

This is the simplest and easiest way to check year is a leap year or not program in python. We will take a year while declaring the variables. Python program to check year is a leap year or not using if-elif-else statements and finally, its result will be displayed on the screen.

# Python program to check year is a leap year or not

# take input
year = int(input('Enter a year: '))

# check leap year or not
if (year % 400) == 0:
    print('{0} is a leap year'.format(year))
elif (year % 100) == 0:
    print('{0} is not a leap year'.format(year))
elif (year % 400) == 0:
    print('{0} is a leap year'.format(year))
else:
    print('{0} is not a leap year'.format(year))

Output for the different input values:-

Enter a year: 2000
2000 is a leap year

Enter a year: 2020
2020 is a leap year

Enter a year: 2021
2021 is not a leap year

In this program, inputs are scanned using the input() function and stored in the variable year.

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

Check year is a leap year or not using if-elif-else statement and finally, the result will be displayed on the screen.

if(year % 400) == 0:
    print('{0} is a leap year'.format(year))
elif(year % 100) == 0:
    print('{0} is not a leap year'.format(year))
elif(year % 400) == 0:
    print('{0} is a leap year'.format(year))
else:
    print('{0} is not a leap year'.format(year))

Check Leap Year using nested if-else

# Python program to check year is a leap year or not

# take input
year = int(input('Enter a year: '))

# check leap year or not
if(year % 4) == 0:
    if(year % 100) == 0:
        if(year % 400) == 0:
            print('{0} is a leap year'.format(year))
        else:
            print('{0} is not a leap year'.format(year))
    else:
        print('{0} is a leap year'.format(year))
else:
    print('{0} is not a leap year'.format(year))

Output:-

Enter a year: 2022
2022 is not a leap year

Leap Year in Python using Function

We can also take the help of a function to check leap year in python. A function is a block of code that performs a specific task.

# Python program to check year is a leap year or not using function

def checkYear(year):   #user-defined function
    # check leap year or not
    if (year % 4) == 0:
        if (year % 100) == 0:
            if (year % 400) == 0:
                return True
            else:
                return False
        else:
             return True
    else:
        return False

# take input
year = int(input('Enter a year: '))

# function call and display result
if(checkYear(year)):
    print('{0} is a leap year'.format(year))
else:
    print('{0} is not a leap year'.format(year))

Output:-

Enter a year: 2003
2003 is not a leap year

In this program, we will be the first defined function.

def checkYear(year):   #user-defined function
    # check leap year or not
    if (year % 4) == 0:
        if (year % 100) == 0:
            if (year % 400) == 0:
                return True
            else:
                return False
        else:
             return True
    else:
        return False

Inputs are scanned using the input() function and stored in variable year. Then call the function and finally, its result will be displayed on the screen.

if(checkYear(year)):
print('{0} is a leap year'.format(year))
else:
print('{0} is not a leap year'.format(year))

Leap Year Logic in a Single Line

The logic of the checkYear() method also can be written in one line as,

# Python program to check leap year or not in a single line

def checkYear(year):
    # check leap year or not
    return ((year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0))
	
# take input
year = int(input('Enter a year: '))

# function call and display result
if(checkYear(year)):
    print('{0} is a leap year'.format(year))
else:
    print('{0} is not a leap year'.format(year))

Output:-

Enter a year: 2024
2024 is a leap year

Return true if the year is a multiple of 4 and not multiple of 100 Or year is multiple of 400.

return ((year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0))

Leap Year Code in Python using Calendar

In this program, We can check year is a leap year or not using the calendar. The calendar will be imported from the python library.

# Python program to check leap year or not using calender

def checkYear(year):
    # check leap year or not
    import calendar
    return(calendar.isleap(year)) 	
# take input
year = int(input('Enter a year: '))

# function call and display result
if(checkYear(year)):
    print('{0} is a leap year'.format(year))
else:
    print('{0} is not a leap year'.format(year))

Output:-

Enter a year: 2028
2028 is a leap year

Also See:- Python Program to Find Largest of 3 Numbers

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *