Python Program to Check Leap Year

Python Program to Check Leap Year

2 mins read3.2K Views Comment
Updated on Nov 25, 2022 19:24 IST

Do you want to know how to check leap year using python? Then this article will provide you three methods to find it out.

2022_07_MicrosoftTeams-image-2.jpg

In this article, we will explore the Program to Check Leap Year using Python programming. Here’s the plan!


What is a Leap year?
Program -1 using a single line of code
Program -2 using looping methods
Program-3 uses the inbuilt library in Python
Conclusion

What is a Leap year?

A leap year is a year in which there is one extra day, bringing the total number of days in the year to 366. An extra day is added in February, making the month 29 days long. As you might already know, there is a leap year every four years.

You should follow the syntax explained below to find whether a year is a leap year or not!

  1. If a year is divisible evenly by four, there will be no remainder; thus, proceed to the next step. If it isn’t divisible by four, it isn’t divisible at all. It isn’t a leap year this year. Consider the following scenario: The year 1997 is not a leap year.
  2. If a year can be divided by four but not by one hundred. For instance, 2012 is a leap year. Proceed to the following step if a year is divisible by 4 and 100.
  3. If a year is divisible by 100 but not by 400, it is a leap year. For example, if the year is 1900, it is not a leap year. A leap year is one in which the year is divisible by both. As a result, the year 2000 is a leap year.

Also Read: Python Projects for Beginners

Also Read: Powerful Python Libraries for Data Science and Machine Learning

Ternary Operators in Python
Ternary Operators in Python
For a Python programmer, one of the major concerns is the size and readability of the code. Any efficient program in Python is supposed to be easily readable, short, and...read more
How to Check if Two Python Strings are Anagrams
How to Check if Two Python Strings are Anagrams
An Anagram is a word or phrase that is formed by rearranging all the letters of a different word or phrase exactly once such as elbow and below. In this...read more
Python vs. C++ – What’s the Difference?
Python vs. C++ – What’s the Difference?
Python and C++ both are general-purpose programming languages. However, their usage and syntax differ widely. In this article, we will highlight the most prominent differences between the two languages.

Here’s a simple flowchart.

2022_07_image-140.jpg

In Python, there are several to find out if a given year is a leap year. Let us explore them one by one.

Method 1:

Checking whether the given year is a leap year or not using a single line of code.

Let’s create a function as below,


 
def checkleapyear(y):
return(((y % 4 == 0) and (y % 100 != 0)) or (y % 400 ==0));
#The above line Returns true if the year is a multiple of 4 and not a multiple of 100 OR the year is a multiple of 400 as mentioned per the syntax logic above.
y= 2000 if (checkleapyear(y)):
print("The given year is leap year")
else:
print("The given year is not a leap year")
Copy code
Output:

2022_07_image-168.jpg

Method 2:

Checking whether the given year is a leap year or not using looping methods.

Let’s create a function below and use if-else loop functions to find the leap year as per the above-mentioned logic.


 
def checkleapyear(y):
if y % 4 == 0:
if y % 100 == 0:
if y % 400 == 0:
return True
else:
return False
else:
return True
else:
return False
y= 2001
if (checkleapyear(y)):
print("The given year is leap year")
else:
print("The given year is not a leap year")
Copy code
Output:

2022_07_image-169.jpg

Method 3:

This method uses the inbuilt library in python to find out whether the given year is a leap year or not.


 
#defining a function with the inbuilt library
def checkleapyear(y):
import calendar
return(calendar.isleap(y))
y= 2001
if (checkleapyear(y)):
print("The given year is leap year")
else:
print("The given year is not a leap year")
Copy code

Output:

2022_07_image-171.jpg

This method uses the built-in library in Python to determine whether the given year is a leap year or not.

Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

Free
6 weeks
– / –
2 weeks
– / –
16 weeks
1.7 K
3 months
– / –
– / –
4.24 K
2 weeks
3 K
3 weeks
– / –
4 months

Conclusion

I hope this blog was helpful for you to understand how to check if the given year was a leap year or not!. So in this, we covered the program to check leap year using python by three different methods. If you liked this article hit the like button and share it with your friends.

_______________

Recently completed any professional course/certification from the market? Tell us what you liked or disliked in the course for more curated content.

Click here to submit its review with Shiksha Online

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio