Python Program to Check Leap Year
Do you want to know how to check leap year using python? Then this article will provide you three methods to find it out.
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!
- 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.
- 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.
- 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
Here’s a simple flowchart.
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")
Output:
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= 2001if (checkleapyear(y)): print("The given year is leap year")else: print("The given year is not a leap year")
Output:
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 librarydef checkleapyear(y): import calendar return(calendar.isleap(y))
y= 2001if (checkleapyear(y)): print("The given year is leap year")else: print("The given year is not a leap year")
Output:
This method uses the built-in library in Python to determine whether the given year is a leap year or not.
Best-suited Python courses for you
Learn Python with these high-rated online courses
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
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