Python Program to Check if given year is Leap Year
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:
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.
- Using Modulo Operator
- Using Calendar Module
- Using Lambda Function
- Using any() Method
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
Contact Us