How to Check Leap Year in Python?
|In this Python example, we will discuss different ways by which we can check that the given year is a leap year or not. Let’s begin.
1. What is a Leap Year ?
A leap year or bissextile year is a calendar year that contains an additional day added to keep the calendar year synchronized with the astronomical year or seasonal year.
– Wikipedia
In order to correct and coordinate the world’s system of dating and the solar system’s physical properties, we insert an additional day after a cycle of some years.
Instead of 365 days, each leap year has 366 days, generally, we extend days in February from 28 days to 29 days. These extra days occur in each year which is an integer multiple of 4.
Example: Input : 2020 Output : 2020 is a Leap Year Input : 2011 Output : 2011 is not a Leap Year
Some of the topics which will be helpful for understanding the program implementation better are:
2. Python Program to Check if a Given Year is Leap Year or Not
The year is a leap year if any of the following conditions are satisfied:
- The year is multiple of 400.
- The year is multiple of 4 and not multiple of 100.
Let’s implement the code and see how it works.
# Python program to check leap year or not year = int(input("Enter the year: ")) if (year % 4) == 0: if (year % 100) == 0: if (year % 400) == 0: print(year,"is a leap year") else: print(year,"is not a leap year") else: print(year,"is a leap year") else: print(year,"is not a leap year")
Output Enter the year: 2000 2000 is a leap year
First, we check if the given year is divisible by 4, then if it is divisible by 100 then by 400.
If it is not divisible by 4, then it’s confirmed that it is not a leap year. If it is divisible by 4 but not 100 then it is a leap year. If it is divisible by 4, 100, and 400 then it is a leap year.
#Python Program to Check leap year n = int(input("Enter the number to check if it is Prime or not: ")) if n > 1: for i in range(2, int(n/2)+1): if n % i == 0: print(n, "is not a prime number") break else: print(n, "is a prime number") else: print(n, "is not a prime number")
3. Check for a Leap Year Using Function
Let’s implement the code by defining a function
#Python Program to Check leap year using Function def checkleap(year): return (((year % 4 == 0) and (year % 100 != 0)) or (year % 400 == 0)); year = 2016 if checkleap(year): print(year,"is a Leap Year") else: print(year," is Not a Leap Year")
Inside the checkleap
function, we have given the condition to check in a single line.
4. Check for a Leap Year Using Module
In Python, we have calendar
module which has a method isleap
inside it. The isleap
method return True
, if the year passed as a parameter is a leap year and False
when it is not.
Let’s see how it works
import calendar year = 2010 if calendar.isleap(year): print("Leap Year") else: print("Not a Leap Year")
Output Not a Leap Year
5. Conclusion
In this article, we have learned different ways by which we can check if a given year is a leap year or not. We have covered the basic method to check leap year, checked leap year by defining a function, giving the condition in a single line, and also checked using the calendar module.
Helpful Links
Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.
Also for examples in Python and practice please refer to Python Examples.
Complete code samples are present on Github project.
Recommended Books
An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding
Do not forget to share and Subscribe.
Happy coding!! ?