Python Program to Check Leap Year

In Python programming, checking for a leap year is a common task that involves determining if a given year is divisible by 4, with exceptions for years divisible by 100 unless they are also divisible by 400. Checking for leap years is essential for handling calendar calculations and date-related operations. In this blog post, we will explore how to write a Python program to check for leap years, understand the logic behind it, and provide examples to demonstrate its implementation.

Checking for Leap Years in Python:

To check if a given year is a leap year, we follow specific rules:

  1. If the year is divisible by 4, it may be a leap year.
  2. If the year is divisible by 100, it is not a leap year, unless…
  3. If the year is divisible by 400, it is a leap year.

Example: Python program to check leap year:

def is_leap_year(year):
    if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
        return True
    else:
        return False

year = 2024
if is_leap_year(year):
    print(year, "is a leap year")
else:
    print(year, "is not a leap year")

Output:

2024 is a leap year

Conclusion

Logic for leap years in Python involves applying specific rules based on divisibility. In this blog post, we explored the logic behind checking for leap years and provided an example Python program to demonstrate its implementation. You can test the program with different years to verify their leap year status. Handling leap years is crucial for accurate calendar calculations and date-related applications. Enjoy implementing leap year checks in your Python programs!