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 example. Before we get into Python leap year programs, Let us see the logic and definition behind the Leap Year.

Python 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 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, we have to calculate further to check the Leap year in Python.

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

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 Leap year or not using the If statement.

# using 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 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 in Python.

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

First condition (yr%4 == 0) will check whether the reminder 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 century number.

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

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

Python Program to Check Leap Year using Elif Statement

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

# using 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 program, first, User will enter any number to check whether that is Leap year or Not.

  • First 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 next condition.
  • 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 is failed it came to second condition. If both the first and second condition Fails then it will go to third condition.
  • Third condition in Python leap year program will check whether num mod 4 is equal to 0. If this condition is True then given one is Leap because We already checked for the century years in the previous condition. If all the statements Fails then it will go Else statement at the end.
  • If all the above statements fail then it is not a Python Leap year

Python Program to Check Leap Year using Nested If Statement

This Python leap year program allows the user to enter any number. And then it will check whether the user entered is Leap or not using the Nested If statement.

# using 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 user to enter any value to check whether that is Leap year or Not. First If condition will check whether the reminder 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 check further for the century. So the compiler will go to the Nested If condition.

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

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

This condition check 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 evaluated to True then the given number is a Leap Year.