Python Program to Check Leap Year | Leap Year Program in Python

# Python Program to Check Leap Year | Leap Year Program in Python

Last updated:
2nd May, 2021
Views
4 Mins
View All

## Introduction

A leap year comes once every four years. A leap year has a total of 366 days, unlike a regular year which has 365 days. This article will help you in implementing a program that determines whether a given year is a leap year in Python or not.

## How to Check If a Given Year is a Leap Year in Python?

A year is determined as a leap year in Python if it meets the following conditions:

• If the given year is divisible by four, then it is a leap year. For example, 2004 is a leap year.
• If the given year is a multiple of 4 but not divisible by 100, it is a leap year. For example, 2012 is a leap year.
• If the given year is divisible by both 100 and 400, then it is a leap year. For example, 2000 is a leap year.

Learn data science course from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

## Methods to Determine if a given Year is a Leap Year in Python

In this section, we will discuss the different methods to determine whether a given year is a leap year in Python or not.

Method 1: Determine Leap Year in Python using Math Module in the Program

 import calendar a = int(input(“ Enter the year that you want to check: ”)) num = calendar.isleap(a) if num == True:     print(“The year entered %s is a Leap Year” %a)  else:     print(“The year entered %s is not a Leap Year” %a)

Input: Enter the year that you want to check: 2012

Output: The year entered 2012 is a Leap Year

Method 2: Determine Leap Year in Python using Nested If Condition in the Program

 a = int(input(“ Enter the year that you want to check: ”)) if (a% 4) == 0     if (a % 100) == 0:         if (a % 400) == 0:             print(“The year entered %s is a leap year” %a)         else:             print(“The year entered %s is not a leap year” %a)     else:         print(“The year entered %s is a leap year” %a) else:     print(“The year entered %s is not a leap year” %a)

Input: Enter the year that you want to check: 2005

Output: The year entered 2005 is not a leap year

Method 3: Determine Leap Year in Python using If Condition in the Program

 a = int(input(“ Enter the year that you want to check: ”)) if (( a%400 == 0)or (( a%4 == 0 ) and ( a%100 != 0))):     print(“The year entered %s is a leap year” %a) else:     print(“The year entered %s is not a leap year” %a)

Input: Enter the year that you want to check: 2004

Output: The year entered 2004 is a leap year

Checkout: Python Pattern Programs

upGrad’s Exclusive Data Science Webinar for you –

## Program Explanation

The above program allows the end-user to provide any year as input to the Python program. The program then checks whether the year entered by the user is a leap year or not. After validation, the program prints the output on the console.

## Explore our Popular Data Science Online Courses

 Executive Post Graduate Programme in Data Science from IIITB Professional Certificate Program in Data Science for Business Decision Making Master of Science in Data Science from University of Arizona Advanced Certificate Programme in Data Science from IIITB Professional Certificate Program in Data Science and Business Analytics from University of Maryland Data Science Online Courses

In the above program, we will check three conditions using logical “AND” and “OR” operators. The three conditions are as follows:

• year%400 == 0 – determines if the remainder is equal to 0 or not.
• year%4 == 0 – determines if the remainder is equal to 0 or not.
• year%100 != 0 – determines if the remainder is not equal to 0.

The second and third statement uses an AND operator, which means the result of these two statements should be True for a leap year. If either of the conditions is false, then the program stops because there is no point in checking further. The given year is not a leap year.

## Top Data Science Skills to Learn to upskill

 SL. No Top Data Science Skills to Learn 1 Data Analysis Online Courses Inferential Statistics Online Courses 2 Hypothesis Testing Online Courses Logistic Regression Online Courses 3 Linear Regression Courses Linear Algebra for Analysis Online Courses

The first and the other two conditions use an OR operator that means either of the results should be True. If either of the statements is true, it means the given year is a leap year in Python.

## Conclusion

The code used in the article is only for explanatory purposes. You can modify the statements given in the examples as per your requirements. In this blog, we discussed the leap year in Python program implementation. You can try out the code to strengthen your Python knowledge.

If you are curious to learn about data science, check out IIIT-B & upGrad’s Executive PG Programme in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.

#### Rohit Sharma

Blog Author
Rohit Sharma is the Program Director for the UpGrad-IIIT Bangalore, PG Diploma Data Analytics Program.
Get Free Consultation

Select Course
Select
By clicking 'Submit' you Agree to

## Suggested Blogs

20731
Attending a PySpark interview and wondering what are all the questions and discussions you will go through? Before attending a PySpark interview, it’s

05 Mar 2024

5053
Data science is an important part of many industries today. Having worked as a data scientist for several years, I have witnessed the massive amounts

by Harish K

28 Feb 2024

5138
Data science training is one of the most hyped skills in today’s world. Based on my experience as a data scientist, it’s evident that we are in

by Harish K

28 Feb 2024

5066
A data science course syllabus covers several basic and advanced concepts of statistics, data analytics, machine learning, and programming languages.

by Harish K

28 Feb 2024

17505
Python is one of the most popular programming languages. Despite a transition full of ups and downs from the Python 2 version to Python 3, the Object-

27 Feb 2024

10713
Introduction Data mining is the process in which information that was previously unknown, which could be potentially very useful, is extracted from a

27 Feb 2024

80365
What is a Data Analytics Lifecycle? Data is crucial in today’s digital world. As it gets created, consumed, tested, processed, and reused, data goes

19 Feb 2024

138670
The arrangement of data in a preferred order is called sorting in the data structure. By sorting data, it is easier to search through it quickly and e