Python calendar isleap() Method with Example

Python calendar.isleap() Method: In this tutorial, we will learn about the isleap() method of calendar module in Python with its usage, syntax, and examples. By Hritika Rajput Last updated : April 24, 2023

Python calendar.isleap() Method

The calendar.isleap() method is an inbuilt method of the calendar module, it is used to check whether the given year is a leap year or not. It returns true if the given year is a leap year; false, otherwise.

Module

The following module is required to use isleap() method:

import calendar

Syntax

The following is the syntax of isleap() method:

isleap(year)

Parameter(s)

The following are the parameter(s):

  • year: the year to be checked

Return Value

The function returns true or false based on whether the year is leap or not.

Example of calendar.isleap() Method in Python

# Python program to illustrate the 
# use of isleap() method
  
# importing calendar module 
import calendar 
  
print("Is year 2020 a leap year:", calendar.isleap(2020))
print()

print("Is year 1993 a leap year:", calendar.isleap(1993))
print()

# Print leap if year is a leap year else print not leap
x = calendar.isleap(1234)
if x == True:
    print("leap")
else:
    print("not leap")

Output

Is year 2020 a leap year: True

Is year 1993 a leap year: False

not leap

Example 2

# Python program to illustrate the 
# use of isleap() method

# Printing leap years between a range  

# importing calendar module 
import calendar 

print("Leap years between 1900 to 2000...")  
for year in range(1900, 2000+1):
  if calendar.isleap(year):
    print(year)

Output

Leap years between 1900 to 2000...
1904
1908
1912
1916
1920
1924
1928
1932
1936
1940
1944
1948
1952
1956
1960
1964
1968
1972
1976
1980
1984
1988
1992
1996
2000



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.