Python Program to Check Leap Year

Write a Python Program to Check Leap Year or Not by using the If Statement, Nested If Statement, and Elif Statement with an example. Before we get into the Python leap year program, let us check the logic and see the definition behind this.

Python Leap year logic

The normal one contains 365 days, but the leap year contains 366 days. Logically, All the years that are perfectly divisible by four are called Leap except the century.

Century years mean they end with 00, such as 1200, 1300, 2400, 2500, etc. (Obviously, they are divisible by 100). For these, our Python program has to calculate further to check the Leap year logic.

  • If the century is divisible by 400, then that is a Leap year.
  • If it is not divisible by 400, then that is not.

Python Program to Check Leap Year using If Statement

This Python program allows the user to enter any number and check whether the user entered is a leap year or not using the If statement.

yr = int(input("Please Enter the Number you wish: "))

if (( yr%400 == 0)or (( yr%4 == 0 ) and ( yr%100 != 0))):
    print("%d is a Leap year" %yr)
else:
    print("%d is Not" %yr)
Please Enter the Number you wish: 1200
1200 is a Leap Year
>>> 
Please Enter the Number you wish: 1300
1300 is Not

In this Python leap year program, Since we have to check multiple conditions within one If Statement, we used Logical AND and Logical OR operators. Let us divide the condition to understand it better

1: ( yr%400 == 0) OR

2: ( yr%4 == 0 ) AND

3: ( yr%100 == 0))

Within this Python Leap Year program code, the First condition (yr%400 == 0) will check whether the (yr%400) reminder is exactly equal to 0 or not. As per the algorithm, any number that is divisible by 400 is a Leap year.

Or the Second If statement holds 2 statements with Logical AND operator, so they both have to be TRUE.

The first condition (yr%4 == 0) will check whether the remainder of the (yr % 4) is exactly equal to 0 or not. If the condition is False, then it will exit from the condition because there is no point in checking the other condition. It is definitely not.

And the Second condition will check (yr % 100) reminder is not equal to 0. If it is TRUE, then the given number is not a century number.

As per the algorithm, any number divisible by 4 but not divisible by 100 is a Leap Year.

TIP: Please refer to Logical Operators to understand the functionality of Python Logical And and Logical Or.

Using ternary operator

This example python leap year code is the same as the above but it uses the ternary operator to display the code in one line.

y = int(input("Enter a Year = "))

print(y, "is a leap year" if ((y % 4 == 0 and y % 100 != 0) or y % 400 == 0) else "is not")
Enter a Year = 1300
1300 is not

Leap Year Program in Python using Calendar

This program imports the calendar module and uses the isleap() function to find whether the given year is leap or not.

import calendar

yr = int(input("Enter a Year = "))

if calendar.isleap(yr):
    print(yr, "is a Leap Year.")
else:
    print(yr, "is not.")
Enter a Year = 2020
2020 is a Leap Year.

Python Program to Check Leap Year using Elif Statement

It allows the user to enter any value. And then, this program code will check whether the user entered is Leap year or not using the Python Elif Statement.

ya = int(input("Please Enter as you wish : "))

if (ya%400 == 0):
    print("%d is a Leap year" %ya)
elif (ya%100 == 0):
    print("%d is Not" %ya)
elif (ya%4 == 0):
    print("%d is a Leap year" %ya)
else:
    print("%d is Not" %ya)
Please Enter as you wish : 2022
2022 is Not

=================== RESTART ==================
Please Enter as you wish : 1300
1300 is Not

=================== RESTART ==================
Please Enter as you wish : 1200
1200 is a Leap year

In this Python elif program, first, the User will enter any number to check whether that is Leap Year or Not.

  • First, the If condition will check whether the (ya % 400) reminder is exactly equal to 0. As per the algorithm, any number that is divisible by 400 is a Leap year. If this condition Fails, then it will go to the next condition.
  • The second If condition will check (ya % 100) reminder is exactly equal to 0 or not. As per the algorithm, any number that is not divisible by 400 but divisible by 100 is Not a Century. We checked (ya % 400) in the First If statement. Because it failed, it came to a second condition. If both the first and second condition Fails, then it will go to the third condition.
  • The third condition in the Python leap year program will check whether num mod 4 is equal to 0. If this condition is True, then the given one is Leap because We already checked for the century years in the previous condition. If all the statements Fail, then it will go Else statement at the end.
  • If all the above statements fail, then it is not a Leap year

Python Program to Check leap year using Nested If Statement

This Python leap year program uses nested if statement to check the lear year. It allows the user to enter any number, and then, it will check using the Nested If statement.

num = int(input("Please Enter the Number you wish: "))

if(num%4 == 0):
    if(num%100 == 0):
        if(num%400 == 0):
            print("%d is a Leap Year" %num)
        else:
            print("%d is Not" %num)
    else:
        print("%d is a Leap Year" %num)
else:
    print("%d is Not" %num)
Python Program to Check Leap Year using Nested If Statement

This Python program allows users to enter any value to check whether that is a Leap year or Not. First, the If condition will check whether the remainder of the (num%4) is exactly equal to 0 or not.

  • If the condition is False, the given number is definitely not.
  • If the condition is True, then we have to check further for the century. So the compiler will go to the Nested If condition.

Second If condition in the leap year program will check (num%100) whether the reminder is exactly equal to 0 or Not.

  • If this condition is False, then it is not a century year. So the given number is definitely Leap Year.
  • If the expression is true, then we have to check whether the number is divisible by 400 or not. So the compiler will goto to another Nested If condition.

This condition checks whether the remainder of the (num%400) is exactly equal to 0 or not.

  • If the expression is False, then the given number is definitely not.
  • If the expression is evaluated to True, then the given number is a Leap Year.

Leap Year Program in Python using functions

This function aceepts any integer and using the if else sttaemnet, it checks the condition and find out leap year or not.

def isleap(yr):
    if yr % 400 == 0 or (yr % 4 == 0 and yr % 100 != 0):
        return True
    else:
        return False

yr = int(input("Enter a year = "))

if isleap(yr):
    print(yr, "is a leap year")
else:
    print(yr, "is not")
Enter a year = 1900
1900 is not

Enter a year = 2020
2020 is a leap year

Using lambda function

This program uses the Python lambda function to check whether it is a leap year or not.

yr = int(input("Enter a year = "))

isleap = lambda yr: (yr % 4 == 0 and yr % 100 != 0) or yr % 400 == 0

if isleap(yr):
    print(yr, "is a leap year")
else:
    print(yr, "is not")
Enter a year = 2200
2200 is not

Using & operator

This approch perfroms bit manipulation.

yr = int(input("Enter a year = "))

isleap = (yr & 3 == 0) and ((yr % 25 != 0) or (yr & 15 == 0))

if isleap:
    print(yr, "is a leap year")
else:
    print(yr, "is not")
Enter a year = 1920
1920 is a leap year

Python Program to Check Leap Year using list comprehension

yr = int(input("Enter a year = "))

isleap = any([yr % 400 == 0, yr % 4 == 0 and yr % 100 != 0])

if isleap:
    print(yr, "is a leap year")
else:
    print(yr, "is not")
Enter a year = 2030
2030 is not

Leap Year Program in Python using datetime module

This program uses the datetime module and check whether the second month has 29 days.

import datetime

yr = int(input("Enter a year = "))

try:
    datetime.date(yr, 2, 29)
    print(yr, "is a leap year")
except ValueError:
    print(yr, "is not") 
Enter a yr = 1947
1947 is not

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.