Python Program for calendar isleap() Method with Examples

Calendar Module:

The calendar module allows you to output calendars like a program and includes extra calendar-related operations. Calendar module functions and classes make use of an idealized calendar, the current Gregorian calendar extended in both directions indefinitely.

isleap() Method:

calendar.isleap() is a function in Python’s calendar module for creating simple text calendars.

The isleap() method produces a result. If the year is a leap year, True; otherwise, False.

A leap year is a year with one extra day added to bring the calendar year in synchronization with the astronomical or seasonal year.

Syntax:

isleap(year)

Parameter Values: 

year: This is required. It is a number. The Year to check if it’s a leap year or not.

Return Value:

If the year is a leap year, it returns True. Otherwise, False.

Examples:

Example1:

Input:

Given Year = 2012

Output:

Checking whether the given year 2012  is a leap or not =  True

Example2:

Input:

Given Year = 2017

Output:

Checking whether the given year 2017  is a leap or not =  False

Program for calendar isleap() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import calendar module using the import keyword.
  • Give the year as static input and store it in a variable.
  • Pass the given year as an argument to the isleap() function to check whether the given year is a leap year or not.
  • Store it in another variable.
  • Print the result after checking whether the given year is a leap or not.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Give the year as static input and store it in a variable.
gvn_yr = 2012
# Pass the given year as an argument to the isleap() function to check whether
# the given year is a leap year or not.
# Store it in another variable.
rslt = calendar.isleap(gvn_yr)
# Print the result after checking whether the given year is a leap or not.
print("Checking whether the given year", gvn_yr, " is a leap or not = ", rslt)

Output:

Checking whether the given year 2012  is a leap or not =  True

The isleap() method’s operation is described below

Approach:

  • Import calendar module using the import keyword.
  • Give the year as static input and store it in a variable.
  • Pass the given year as an argument to the isleap() function to check whether the given year is a leap year or not.
  • Store it in another variable.
  • Check if the above result is equal to True using the if conditional statement.
  • If it is True, then Pass some random year, month, width, lines as arguments to the prmonth() method to print the specified month of the given leap year.
  • Else print “The given year is not a Leap Year”.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Give the year as static input and store it in a variable.
gvn_yr = 2012
# Pass the given year as an argument to the isleap() function to check whether
# the given year is a leap year or not.
# Store it in another variable.
rslt = calendar.isleap(gvn_yr)
# Check if the above result is equal to True using the if conditional statement.
if rslt == True:
    # If it is True, then Pass some random year, month, width, lines as
    # arguments to the prmonth() method to print the specified month of the
    # given leap year.
    calendar.prmonth(gvn_yr, 5, 3, 1)

# Else print "The given year is not a Leap Year".
else:
    print("The given year", gvn_yr, "is not a Leap Year")

Output:

          May 2012
Mon Tue Wed Thu Fri Sat Sun
      1   2   3   4   5   6
  7   8   9  10  11  12  13
 14  15  16  17  18  19  20
 21  22  23  24  25  26  27
 28  29  30  31

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import calendar module using the import keyword.
  • Give the year as user input using the int(input()) function and store it in a variable.
  • Pass the given year as an argument to the isleap() function to check whether the given year is a leap year or not.
  • Store it in another variable.
  • Print the result after checking whether the given year is a leap or not.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Give the year as user input using the int(input()) function and store it in a variable.
gvn_yr = int(input("Enter some random year = "))
# Pass the given year as an argument to the isleap() function to check whether
# the given year is a leap year or not.
# Store it in another variable.
rslt = calendar.isleap(gvn_yr)
# Print the result after checking whether the given year is a leap or not.
print("Checking whether the given year", gvn_yr, " is a leap or not = ", rslt)

Output:

Enter some random year = 2017
Checking whether the given year 2017 is a leap or not = False

The isleap() method’s operation is described below

Approach:

  • Import calendar module using the import keyword.
  • Give the year as user input using the int(input()) function and store it in a variable.
  • Pass the given year as an argument to the isleap() function to check whether the given year is a leap year or not.
  • Store it in another variable.
  • Check if the above result is equal to True using the if conditional statement.
  • If it is True, then Pass some random year, month, width, lines as arguments to the prmonth() method to print the specified month of the given leap year.
  • Else print “The given year is not a Leap Year”.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Give the year as user input using the int(input()) function and store it in a variable.
gvn_yr = int(input("Enter some random year = "))
# Pass the given year as an argument to the isleap() function to check whether
# the given year is a leap year or not.
# Store it in another variable.
rslt = calendar.isleap(gvn_yr)
# Check if the above result is equal to True using the if conditional statement.
if rslt == True:
    # If it is True, then Pass some random year, month, width, lines as
    # arguments to the prmonth() method to print the specified month of the
    # given leap year.
    calendar.prmonth(gvn_yr, 5, 3, 1)

# Else print "The given year is not a Leap Year".
else:
    print("The given year", gvn_yr, "is not a Leap Year")

Output:

Enter some random year = 2014
The given year 2014 is not a Leap Year