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

Top 12 Reasons Why Python is So Popular With Developers in 2024
99361
In this article, Let me explain you the Top 12 Reasons Why Python is So Popular With Developers. Easy to Learn and Use Mature and Supportive Python C
Read More

by upGrad

31 Jul 2024

Priority Queue in Data Structure: Characteristics, Types & Implementation
57691
Introduction The priority queue in the data structure is an extension of the “normal” queue. It is an abstract data type that contains a
Read More

by Rohit Sharma

15 Jul 2024

An Overview of Association Rule Mining & its Applications
142465
Association Rule Mining in data mining, as the name suggests, involves discovering relationships between seemingly independent relational databases or
Read More

by Abhinav Rai

13 Jul 2024

Data Mining Techniques & Tools: Types of Data, Methods, Applications [With Examples]
101802
Why data mining techniques are important like never before? Businesses these days are collecting data at a very striking rate. The sources of this eno
Read More

by Rohit Sharma

12 Jul 2024

17 Must Read Pandas Interview Questions & Answers [For Freshers & Experienced]
58170
Pandas is a BSD-licensed and open-source Python library offering high-performance, easy-to-use data structures, and data analysis tools. The full form
Read More

by Rohit Sharma

11 Jul 2024

Top 7 Data Types of Python | Python Data Types
99516
Data types are an essential concept in the python programming language. In Python, every value has its own python data type. The classification of dat
Read More

by Rohit Sharma

11 Jul 2024

What is Decision Tree in Data Mining? Types, Real World Examples & Applications
16859
Introduction to Data Mining In its raw form, data requires efficient processing to transform into valuable information. Predicting outcomes hinges on
Read More

by Rohit Sharma

04 Jul 2024

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

04 Jul 2024

Most Common Binary Tree Interview Questions & Answers [For Freshers & Experienced]
10561
Introduction Data structures are one of the most fundamental concepts in object-oriented programming. To explain it simply, a data structure is a par
Read More

by Rohit Sharma

03 Jul 2024

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