
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check Leap Year Using Python
A year that occurs every four years is considered a leap year in the calendar. For example, 2024 is divisible by 4 therefore, it is a leap year.
Conditions for a Leap Year
To determine whether a given year is a leap year, the following conditions must be met ?
- The year must be divisible by 4.
- If the year is divisible by 100, it must also be divisible by 400 to qualify as a leap year.
- If the year is divisible by 100 but not by 400, it is not a leap year.
Finding Leap Year in Python
Following are different ways to check whether a given year is a loop year in Python -
- Using if-else Statement
- Using Calendar Module
- Using Custom Function
Let us look at these one by one -
Using 'if-else' Statement
We can check if a given year is a leap year using an if-else statement. The conditions are placed in the if statement. If the conditions are satisfied, the program returns "It is a leap year." Otherwise, it returns "It is not a leap year."
Example
In the following example we have checked whether a given year is a leap year or not using an if-else statement ?
Year = 2024 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")
Following is the output of the above code ?
2024 is a leap year
Using Calendar Module
We can also check whether a year is a leap year by using Python's calendar module. The isleap() function determines if a year is a leap year.
Example
Here's an example of checking if a given year is a leap year using the calendar module ?
import calendar Year = 2025 if calendar.isleap(Year): print(Year, "is a leap year") else: print(Year, "is not a leap year.")
Following is the output of the above code ?
2025 is not a leap year
Using Custom Function
We can also determine whether a given year is a leap year using a function. If the year satisfies the leap year conditions, the function will return True; otherwise, it will return False.
Example
In the following example we have checked if a given year is a leap year or not using a function ?
def Leap_year(Year): if Year % 4 == 0 and (Year % 100 != 0 or Year % 400 == 0): return True else: return False Year = 2026 Result = Leap_year(Year) print(Year, "is a Leap Year:", Result)
Following is the output of the above code ?
2026 is a Leap Year: False