Python Program to check whether the Year is Leap Year

8/6/2022
All Articles

check leap year Python

Python Program to check whether the Year is Leap Year

Python Program to Check Whether a Year is a Leap Year

Introduction

A leap year is a year that is evenly divisible by 4, except for end-of-century years, which must also be divisible by 400. This means:

  1. If a year is divisible by 400, it is a leap year (e.g., 1600, 2000, 2400).
  2. If a year is divisible by 4 but not by 100, it is a leap year (e.g., 2020, 2024, 2028).
  3. Any other year is not a leap year.

In this article, we will write a Python program to determine whether a given year is a leap year or not.

Python Program to Check for a Leap Year

def is_leap_year(year):
    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
        print(year, "is a leap year")
    else:
        print(year, "is not a leap year")

# Taking user input
year = int(input("Enter the year: "))
is_leap_year(year)

Explanation of the Code

  • The function is_leap_year(year) checks if the year is divisible by 4 but not by 100, or if it is divisible by 400.
  • The input() function allows the user to enter a year.
  • The function prints whether the entered year is a leap year or not.

Example Outputs

Case 1:

Enter the year: 2000
2000 is a leap year

Case 2:

Enter the year: 1600
1600 is a leap year

Case 3:

Enter the year: 2020
2020 is a leap year

Case 4:

Enter the year: 2019
2019 is not a leap year

Key Takeaways

  • A leap year occurs every 4 years but not every 100 years unless also divisible by 400.
  • The Python program correctly determines whether a year is a leap year using simple conditional logic.
  • This program is useful in calendar applications and date calculations.

Conclusion

Checking for a leap year in Python is simple and efficient using modulus operations. This program can be extended for date-based calculations in real-world applications.

Article