Python Program to Check Leap Year

In this tutorial, we will discuss a Python Program to check Leap Year.

Before going to the program first, let us understand what is a Leap Year.

Leap Year:

  • A leap year is a year that is divisible by 4, but if it is a century year (ending in 00), it must also be divisible by 400.
  • For example, 2000 and 2020 are leap years, but 1900 is not.

Related: Python Program to find the Largest of Three Numbers

Program code for checking leap year using Python

# Leap Year Checker in Python
def is_leap_year(year):
    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
        return True
    else:
        return False

year = int(input("Enter a year: "))
if is_leap_year(year):
    print(f"{year} is a leap year.")
else:
    print(f"{year} is not a leap year.")

Explanation

  1. Function Definition: The is_leap_year function takes an integer year as input and returns True if the year is a leap year, and False otherwise.
  2. Leap Year Check: The function checks if the year is divisible by 4 and not by 100, or if it is divisible by 400.
  3. Main Program: The program prompts the user to enter a year and then checks if it is a leap year using the is_leap_year function.

Output

Python Program to Check Leap Year

  • When you run the above program, it will prompt you to enter a year.
  • After entering the year, it will check whether the year is a leap year or not and print the result.

Conclusion

  • In this tutorial, we learned how to check if a given year is a leap year using a Python program.
  • Understanding this concept is essential for solving various calendar-related problems and competitive programming challenges.
  • Practice this example to enhance your programming skills and understanding of leap year calculations.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *