The leap year program is a classic programming question and is often asked in technical interviews. Today we will explore how we can write a leap year program in Python using different approaches, from simple if-else statements to even an in-built method. Let’s begin.
Before we go ahead and build our leap year program in Python, let’s first take a minute to understand the logic. Understanding the underlying logic behind your application is integral to improving your skills as a Python developer.
Condition | Leap Year(Y/N) |
Divisible by 4 but not by 100 | Y |
Divisible by 100 but not by 400 | N |
Divisible by 400 | Y |
Not divisible by 4 | N |
This rule accurately handles special cases, including century years like 1900 and 2000.
Python Program to Check Leap Year
Now that we have an understanding of the logic used, let’s jump to some leap year code in Python.
1. Using an if-else Statement
Here’s how the logic can be written in Python using basic conditionals:
Code:
Output:
This one-liner condition covers all valid leap year scenarios, and you’ll see it used in most of the leap year Python programs later in this guide. Try changing the year to test it out.
2. Using nested if-else
You can use this approach to gain a deeper understanding of flow control as it uses nested if-else statements.
Code:
Output:
3. Using the ternary operator
Python allows conditional expressions in a single line for even more efficiency.
Code:
Output:
This is Python code for leap year identification; it is basically an if-else statement, but using more convenient syntax.
4. Using the calendar module
Python has a built-in calendar module that provides a helpful method that can provide the desired results without any extra logic.
Code:
Output:
5. Using a lambda function
Useful when passing logic around in functional programming styles.
Code:
Output:
Creating a Reusable Leap Year Function in Python
So far, we have covered the logic of identifying a leap year in Python and also how to do it using different programming approaches. But for large-scale projects, it is advised that you wrap your leap year logic inside a function. This makes the code more modular, testable, and reusable.
Code:
This is how the logic would be implemented in a real-world application. This function returns True if the input year is a leap year and False otherwise.
Now that you have all the necessary tools, let’s build an interactive program to find a leap year in Python. We will accept input from the user and calculate whether it is a leap year or not dynamically.
Code:
Don’t forget to provide a number in the input section of the code lab to get the desired output. Pay attention to how we are inverting the input into an int. This is because input() returns a string by default, and as you know, we cannot run arithmetic operations on a string.
Testing Python Leap Year Program with Edge Cases
Testing your application is integral to determining whether it works or not. In some cases, you might get the output you want, but it is important to check it with more edge cases to make sure that there are no loopholes in your logic. Try the following test cases, and if your application gives you the desired results, congratulations, you have successfully built a Python program to check for a leap year.
Test Cases:
Year | Expected Result | Explanation |
2024 | Leap Year | Divisible by 4, not by 100 |
1900 | Not a Leap Year | Divisible by 100, not by 400 |
2000 | Leap Year | Divisible by 400 |
2100 | Not a Leap Year | Divisible by 100, not by 400 |
2400 | Leap Year | Divisible by 400 |
2023 | Not a Leap Year | Not divisible by 4 |
Conclusion
Determining whether a year is a leap year in Python is a classic logic problem that helps reinforce understanding of conditionals, modular arithmetic, and date handling. In this blog, we have covered all the possible approaches that you can take in Python to solve this problem, from if-else statements to the calendar module, which is very convenient. For most applications, a reusable function or the built-in calendar.isleap() method is the best way to go. But knowing the raw logic helps build your foundation and prepares you for interviews or logic-heavy programming tasks. Check out our complete Python certification course to learn more advanced Python concepts and logic. You can also explore how we can apply the same logic to solve the same leap year problem in C.
FAQs- Leap Year Program in Python
1. What is the correct leap year condition in Python?
The standard leap year logic in Python is,
(year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
This condition ensures that century years like 1900 are correctly excluded, and years like 2000 are included.
2. How do I check if a year is a leap year using Python’s calendar module?
Python’s built-in calendar module provides a convenient way to check if a year is a leap year.
import calendar
calendar.isleap(2024) # Returns True
3. Why is 1900 not a leap year, but 2000 is?
1900 is divisible by 100 but not 400, and hence it is not a leap year.
2000 is divisible by 400 and, therefore, is a leap year.
4. What happens if I input a negative year or zero?
The leap year logic will still apply mathematically, but the output will not be accurate, as there are no negative years on the calendar.
5. How can I find all leap years in a given range using Python?
You can use a simple loop with calendar.isleap() to filter leap years in a range:
import calendar
leap_years = [year for year in range(2000, 2051) if calendar.isleap(year)]
print(leap_years)
Output:
[2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, 2044, 2048]