Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconData Sciencebreadcumb forward arrow iconPython 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
Read Time
4 Mins
share image icon
In this article
Chevron in toc
View All
Python Program to Check Leap Year | Leap Year Program in Python

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

Related Read: Python Program to add two numbers

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 –

How upGrad helps for your Data Science Career?

 

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

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

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.

Check out Python Tutorials for free.

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.

Profile

Rohit Sharma

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

Explore Free Courses

Suggested Blogs

Most Common PySpark Interview Questions & Answers [For Freshers & Experienced]
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
Read More

by Rohit Sharma

05 Mar 2024

Data Science for Beginners: A Comprehensive Guide
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
Read More

by Harish K

28 Feb 2024

6 Best Data Science Institutes in 2024 (Detailed Guide)
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
Read More

by Harish K

28 Feb 2024

Data Science Course Fees: The Roadmap to Your Analytics Career
5066
A data science course syllabus covers several basic and advanced concepts of statistics, data analytics, machine learning, and programming languages.
Read More

by Harish K

28 Feb 2024

Inheritance in Python | Python Inheritance [With Example]
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-
Read More

by Rohan Vats

27 Feb 2024

Data Mining Architecture: Components, Types & Techniques
10713
Introduction Data mining is the process in which information that was previously unknown, which could be potentially very useful, is extracted from a
Read More

by Rohit Sharma

27 Feb 2024

6 Phases of Data Analytics Lifecycle Every Data Analyst Should Know About
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
Read More

by Rohit Sharma

19 Feb 2024

Sorting in Data Structure: Categories & Types [With Examples]
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
Read More

by Rohit Sharma

19 Feb 2024

Data Science Vs Data Analytics: Difference Between Data Science and Data Analytics
68777
Summary: In this article, you will learn, Difference between Data Science and Data Analytics Job roles Skills Career perspectives Which one is right
Read More

by Rohit Sharma

19 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon