# 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

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!
{"ft_lang_label":"__label__en","ft_lang_prob":0.7175442,"math_prob":0.9140408,"size":5043,"snap":"2023-40-2023-50","text_gpt3_token_len":1425,"char_repetition_ratio":0.23179202,"word_repetition_ratio":0.46103182,"special_character_ratio":0.32778108,"punctuation_ratio":0.11678832,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96731955,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-06T04:13:55Z\",\"WARC-Record-ID\":\"<urn:uuid:84908b42-a7d6-4685-b5ea-4964c539d749>\",\"Content-Length\":\"113874\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:01884038-348e-4507-b85d-a428c6c83c8b>\",\"WARC-Concurrent-To\":\"<urn:uuid:ca08099f-5114-433b-81e4-4a0959a6afae>\",\"WARC-IP-Address\":\"104.21.63.95\",\"WARC-Target-URI\":\"https://www.knowprogram.com/python/leap-year-program-python/\",\"WARC-Payload-Digest\":\"sha1:PV2LDI4EBZPE7TM62FHBALR63NOR4RWA\",\"WARC-Block-Digest\":\"sha1:CDVN23X3YJIKZFK77A4BQPZNLMFA3TLY\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100583.13_warc_CC-MAIN-20231206031946-20231206061946-00070.warc.gz\"}"}