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 leap year programs, Let us see the logic and definition behind this.

Leap year Program

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.

  • 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 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 program, 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.

Python Program to Check Leap Year using Elif Statement

It allows the user to enter any value. And then, this program 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 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 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 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 leap year program using Nested If Statement

This leap year program using nested if 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 Leap year or Not. First, 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.