Python program to check the given year is a leap year or not

Leap year program in Python: Here, we will learn how to check a given year is a leap year or not in Python programming language? By Bipin Kumar Last updated : April 09, 2023

A leap year is a year that is completely divisible by 4 except the century year (a year that ended with 00). A century year is a leap year if it is divisible by 400.

Problem statement

Here, a year is provided by the user and we have to check whether the given year is a leap year or not.

Example

Consider the below example with sample input and output:

Input:
Enter the value of year: 2020

Output:
The given year is a leap year.

Input:
Enter the value of year: 2021

Output:
The given year is a non-leap year.

Checking leap year in python

To check leap year in Python, we have two approaches: By using calendar.isleap() method and by using the conditional statements.

  1. By using the calendar module
  2. By simply checking the leap year condition

1) Check leap year by using the calendar module

Before going to solve the problem, initially, we learn a little bit about the calendar module. Calendar module is inbuilt in Python which provides us various functions to solve the problem related to date, month and year.

Python program to check leap year by using the calendar module

# importing the module
import calendar

# input the year 
year=int(input('Enter the value of year: '))
leap_year=calendar.isleap(year)

# checking leap year
if leap_year: # to check condition
    print('The given year is a leap year.')
else:
    print('The given year is a non-leap year.')

Output

RUN 1:
Enter the value of year: 2020
The given year is a leap year.

RUN 2:
Enter the value of year: 2021
The given year is a non-leap year.

2) Check leap year by simply checking the leap year condition

As we know the condition to check the given year is a leap year or not. So, here we will implement the condition and try to write the Python program.

Python program to check leap year by checking the condition

# input the year
y=int(input('Enter the value of year: '))

# To check for non century year
if y%400==0 or y%4==0 and y%100!=0: 
    print('The given year is a leap year.')
else:
    print('The given year is a non-leap year.')

Output

RUN 1:
Enter the value of year: 2020
The given year is a leap year.

RUN 2:
Enter the value of year: 2000
The given year is a leap year.

Python Basic Programs »


Related Programs

Comments and Discussions!

Load comments ↻






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