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.

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

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.

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. 

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.

Prepare for a Career of the Future

UPGRAD AND IIIT-BANGALORE'S EXECUTIVE PG PROGRAMME IN DATA SCIENCE
ENROLL NOW @ UPGRAD

0 replies on “Python Program to Check Leap Year | Leap Year Program in Python”

×