Open In App

Python Program to Check if given year is Leap Year

Last Updated : 08 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Leap years play a crucial role in our calendar system, ensuring that our calendars stay in sync with the Earth’s revolutions around the sun. In this article, we will delve into Python programming to create a simple yet effective program to check if a given year is a leap year.

Main Logic

The determination of leap years involves a set of rules that can be expressed mathematically. The main logic behind leap year detection is based on the following criteria:

  • If a year is divisible by 4, it is a leap year.
  • However, if that year is divisible by 100, it is not a leap year, unless…
  • It is also divisible by 400, in which case, it is a leap year.

Expressed in mathematical form, the conditions can be written as follows:

(year \% 4 = 0) \ \&\& \ ((year \% 100 \neq 0) \ \|\| \ (year \% 400 = 0))

Python Program To Check If A Given Year Is Leap Year

Below, are the methods for Python programs to Check If A Given Year Is a Leap Year.

Check If A Given Year Is Leap Year Using Modulo Operator

In this example, The function `is_leap_year_modulo` checks if a given year is a leap year using the modulo operator to verify divisibility by 4, excluding years divisible by 100 unless divisible by 400. The provided example checks if the year 2024 is a leap year.

Python3

def is_leap_year_modulo(year):
    return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
 
# Example
year_to_check = 2024
result_modulo = is_leap_year_modulo(year_to_check)
print(f"{year_to_check} is a leap year: {result_modulo}")

                    

Output
2024 is a leap year: True

Check If A Given Year Is Leap Year Using Calendar Module

In this example, The code uses Python’s `calendar` module to determine whether a given year is a leap year by invoking the `isleap` function. In the provided example, the year 2024 is checked.

Python3

import calendar
 
def is_leap_year_calendar(year):
    return calendar.isleap(year)
 
# Example
year_to_check = 2024 
result_calendar = is_leap_year_calendar(year_to_check)
print(f"{year_to_check} is a leap year: {result_calendar}")

                    

Output
2024 is a leap year: True

Check If A Given Year Is Leap Year Using Lambda Function

In this example, the lambda function `is_leap_year_lambda` checks if a given year is a leap year using a concise expression with the modulo operator. In the provided example, the year 2023 is checked, and the result (True or False) is printed indicating whether it is a leap year or not.

Python3

is_leap_year_lambda = lambda year: (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
 
# Example
year_to_check = 2023
result_lambda = is_leap_year_lambda(year_to_check)
print(f"{year_to_check} is a leap year: {result_lambda}")

                    

Output
2023 is a leap year: False

Check If A Given Year Is Leap Year Using any() Method

In this example, The function `is_leap_year` checks if a given year is a leap year using a list comprehension and the `any` function to evaluate conditions involving the modulo operator. In the provided example, the year 2018 is checked, and the result (True or False) is printed indicating whether it is a leap year or not.

Python3

def is_leap_year(year):
    return any([(year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)])
 
# Example
year_to_check = 2018
result_list = is_leap_year(year_to_check)
print(f"{year_to_check} is a leap year: {result_list}")

                    

Output
2018 is a leap year: False



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads