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
- Function Definition: The
is_leap_year
function takes an integeryear
as input and returnsTrue
if the year is a leap year, andFalse
otherwise. - Leap Year Check: The function checks if the year is divisible by 4 and not by 100, or if it is divisible by 400.
- 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
- 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.