Leap year Program in Python
A leap year is a year that has 366 days instead of the usual 365 days. It occurs approximately every four years to account for the extra fraction of a day it takes for the Earth to orbit the Sun. In leap years, February has 29 days instead of 28.
To check if a given year is a leap year in Python, you can follow this algorithm:
- Check if the year is divisible by 4. If it is not, then it is not a leap year.
- If the year is divisible by 4, check if it is also divisible by 100. If it is, proceed to the next step; otherwise, it is a leap year.
- If the year is divisible by both 4 and 100, check if it is divisible by 400. If it is, then it is a leap year; otherwise, it is not.
By following these steps, you can accurately determine if a given year is a leap year or not in Python.
The logic behind this algorithm is to consider the special cases of century years (divisible by 100). While most years divisible by 4 are leap years, century years are an exception unless they are also divisible by 400.
This adjustment accounts for the slight discrepancy between the actual length of a year (365.2425 days) and the approximation used in our calendar system (365.25 days).
Pre-requisite
Check Whether a Year is a Leap Year or Not in Python
1. Using The Nested-if Condition in Python
The program uses nested if statements to determine whether the given year is a leap year, following the step-by-step algorithm explained above:
Code:
Output:
Explanation: In the provided code, 2000 and 2016 are considered leap years because they are divisible by 4 and also by 400 (2000 is divisible by 4, 100, and 400; 2016 is divisible by 4 but not by 100, so the leap year condition is satisfied).
However, 2100 is not a leap year because although it is divisible by 4, it is also divisible by 100 but not by 400, failing the leap year condition for century years.
2. Using the if-else Condition in Python
In the following set of programs, we will use the logic studied above to check whether the given year is a Leap year or not.
Looking at the algorithm we’ve studied above we can formulate two different cases where the given year will definitely be a Leap year, i.e.,
- If the year is divisible by 4, and it should be a non-century year, i.e., it is not divisible by 100.
- If the year is divisible by 400 (then it will also be divisible by 100), hence we clearly can say that it is a century year, and also a Leap year.
Code:
Output:
3. Using the Calendar Module in Python
In the following set of programs, we will be using the isleap() function in the Calendar module of Python, to find whether a given year is a Leap year or not. We will first import the Calendar module and we will use the isleap() function to write our code. The isleap() function will return True if the given year is a Leap year else it will return False. Let us now see the program.
Code:
Output:
4. Using the Built-in divmod() Function
We can also use divmod() function in association with if-else to check if an year is a leap year on not.
Code:
Output:
5. Using Pandas Library's is_leap_year Property.
We can also use Pandas library's is_leap_year property to check if an year is a leap year or not.
Code:
Output:
Conclusion
Now that we have discussed the logic, and also the various ways of writing the program for calculating the Leap Year, let us revise a few points:
- In all the programs for checking leap year, the crux remains the same, i.e., first, we decide whether the given year is a century year or not, since the algorithm for both the cases is different.
- Next, for century years we have to check its divisibility with 400. If it gets completely divided, we can say it is a Leap Year, else it is not a Leap Year.
- If the year is a non-century year, then we check its divisibility with 4. If it gets completely divided, we can say it is a Leap Year, else it is not a Leap Year.
- If none of these above conditions are satisfied, we can say that it is not a Leap Year.