In this post, we will learn how to check whether a year is a leap year or not using Python Programming language.
A leap year has 366 days (the extra day is the 29th February). It comes after every four years.
To check whether a year is a leap or not, divide the year by 4. If it is fully divisible by 4, then it is a leap year.
Note: To check years like 1200, 1300, 1500, 2700 etc., these years need to be divided by 400 to check whether they are leap or not.
So, without further ado, let’s begin this tutorial.
Python Program to Check Leap Year
# Python Program to Check Leap Year year = int(input("Enter the year: ")) # Checking Leap Year if year % 4 == 0 and year % 100 != 0: print(year, "is a leap year.") elif year % 400 == 0: print(year, "is a leap year.") elif year % 100 == 0: print(year, "is not a leap year.") else: print(year, "is not a leap year.")
Output 1
Enter the year: 2012
2012 is a leap year.
Output 2
Enter the year: 2030
2030 is not a leap year.
How Does This Program Work ?
year = int(input("Enter the year: "))
The user is asked to enter the year.
if year % 4 == 0 and year % 100 != 0: print(year, "is a leap year.")
Then, we use the if statement to check whether the year is divisible by 4 and not divisible by 100. If the statement is true, then the entered year is a leap year.
elif year % 400 == 0: print(year, "is a leap year.")
If the year is divisible by 400, then it’s a leap year.
elif year % 100 == 0: print(year, "is not a leap year.")
However, if the year is divisible by 100, then the year is not a leap year.
else: print(year, "is not a leap year.")
And in the end, if the year is not divisible by 4, then the entered year is not a leap year.
Conclusion
I hope after going through this post, you understand how to check whether a year is a leap year or not using Python Programming language.
If you have any doubt regarding the topic, feel free to contact us in the comment section. We will be delighted to solve your query.
Also Read: