Python calendar.isleap() Function



The Python calendar.isleap() function is used to determine whether a given year is a leap year.

A leap year is a year that is evenly divisible by 4, except for years that are evenly divisible by 100 unless they are also evenly divisible by 400.

Syntax

Following is the syntax of the Python calendar.isleap() function −

calendar.isleap(year)

Parameters

This function accepts an integer as a parameter representing the year to check.

Return Value

This function returns a boolean value −

  • True: If the given year is a leap year.
  • False: If the given year is not a leap year.

Example: Checking a Leap Year

In this example, we check whether 2024 is a leap year using the calendar.isleap() function −

import calendar

# Check if 2024 is a leap year
is_leap = calendar.isleap(2024)

print("Is 2024 a leap year?", is_leap)  

We get the output as shown below −

Is 2024 a leap year? True

Example: Checking a Non-Leap Year

Now, we use the calendar.isleap() function to check whether 2023 is a leap year −

import calendar

# Check if 2023 is a leap year
is_leap = calendar.isleap(2023)

print("Is 2023 a leap year?", is_leap) 

Following is the output of the above code −

Is 2023 a leap year? False

Example: Checking a Century Year

Here, we are checking whether the year 1900, which is divisible by 100 but not by 400, is a leap year −

import calendar

# Check if 1900 is a leap year
is_leap = calendar.isleap(1900)

print("Is 1900 a leap year?", is_leap)  

Following is the output obtained −

Is 1900 a leap year? False

Example: Checking a Leap Century Year

In this example, we are checking the year 2000, which is divisible by 400, and is a leap year −

import calendar

# Check if 2000 is a leap year
is_leap = calendar.isleap(2000)

print("Is 2000 a leap year?", is_leap)  

The result produced is as shown below −

Is 2000 a leap year? True
python_date_time.htm
Advertisements