Python Program to Check Leap Year (Naive & Calendar Library)

This python program checks whether a given year by user is leap year or not. In this example, first, we check for leap using naive approach and then we check for leap using calendar library in python.

Hint: A year which is divisible by 400, or, a year divisible by 4 but not divisible 100, is a leap year.

Naive Method


# Python program to check leap year

# Reading year from user 
year = int(input('Enter year: '))

# Checking for loop and taking decision
if (year%400==0) or (year%4==0 and year%100!=0):
    print('LEAP YEAR')
else:
    print('NOT LEAP YEAR')

Output

Run 1:
------------------
Enter year: 2019
NOT LEAP YEAR

Run 2:
------------------
Enter year: 2024
LEAP YEAR

Using Calendar Library

In this method, we first import calendar library usingimport calendar. This is standard library in python. After that we call isleap method by passing year given by user i.e. calendar.isleap(year)

Method isleap() returns True if it is leap year and returns False if it is not leap year.


# Python program to check leap year

# importing calendar library
import calendar

# Reading year from user 
year = int(input('Enter year: '))

# Calling isleap() method on calendar library
isLeap = calendar.isleap(year)

# Taking decision
if isLeap:
    print('LEAP YEAR')
else:
    print('NOT LEAP YEAR')

Output

Run 1:
------------------
Enter year: 2019
NOT LEAP YEAR

Run 2:
------------------
Enter year: 2024
LEAP YEAR