Leap Year Program in Python – Explained

We use leap years to keep our calendar in sync with the seasons. How do leap years work, and how often do they occur?

Leap years are years where an extra, or intercalary, the day is added to the end of the shortest month, February. The intercalary day, February 29, is commonly referred to as leap day.

Simply a leap year is a year with an extra day on February 29. Which is added nearly every four years to the calendar year.

The quadratic equations are second-degree equations in x that have two answers for x. These two answers for x are also called the roots of the quadratic equations and are designated as (α, β).

Why Are Leap Years Necessary?

Leap days keep our modern-day Gregorian calendar in alignment with Earth’s revolutions around the Sun.

It takes Earth approximately 365.242189 days, or 365 days, 5 hours, 48 minutes, and 45 seconds, to circle once around the Sun. This is called a tropical year, and it starts on the March equinox.

However, the Gregorian calendar has only 365 days in a year.

If we didn’t add a leap day on February 29 almost every four years, each calendar year would begin about 6 hours before the Earth completes its revolution around the Sun (see illustration).

As a consequence, our time reckoning would slowly drift apart from the tropical year and get increasingly out of sync with the seasons. With a deviation of approximately 6 hours per year, the seasons would shift by about 24 calendar days within 100 years.

Allow this to happen for a while, and Northern Hemisphere dwellers will be celebrating Christmas in the middle of summer in a matter of a few centuries.

Leap days fix that error by giving Earth the additional time it needs to complete a full circle around the Sun.

We want you to know about the Leap Year Program in Python, hence we are come up with the best video explanation you have ever watched on the internet, where you not only understand the logic but also code to get the desired output.

You still face difficulties in understanding the logic? That’s perfectly fine, we have something more for you. You can scroll down to learn more about the this topic.

For all the practice Videos and Explanation on Python please click over here. Python Practice Series.

Video Explanation to Leap Year Program in Python

Code: Leap Year Program in Python

Leap Year Rules: How to Calculate Leap Years

You should follow the following steps to determine whether a year is a leap year or not.

  1. If a year is evenly divisible by 4 means having no remainder then go to the next step. If it is not divisible by 4. It is not a leap year. For example, 1997 is not a leap year.
  2. If a year is divisible by 4, but not by 100. For example 2012, it is a leap year. If a year is divisible by both 4 and 100, go to the next step.
  3. If a year is divisible by 100, but not by 400. For example 1900, then it is not a leap year. If a year is divisible by both, then it is a leap year. So 2000 is a leap year.

Let’s check the program.

Method 1: Leap Year Program in Python

Source Code and Output

year = int(input("Enter a Year:"))

if(year % 4) == 0:
    if(year % 100) == 0:
        if(year % 400) == 0:
            print("{0} is a leap year". format(year))
        else:
            print("{0} is not a leap year". format(year))
    else:
        print("{0} is a leap year". format(year))
else:
    print("{0} is not a leap year". format(year))

Output:

Enter a Year:2000
2000 is a leap year

Code Explanation Method 1: Leap Year Program in Python

Here we are accepting a year from the user, then convert the same into integer and we have stored value in variable year.

Next we have an if statement to check whether a year is divisible by 4  or not, here we are using the modulus operator for it.

If year is divisible by 4 then we will have another if condition, else we will print year is not a leap year

Do you remember 1st rules to calculate leap year, that year must be divisible by 4.

Inside the condition we have also checked if the year is divisible by 100 or not. If a year is divisible by 4 and not by 100 then it’s a leap year else it is not a leap year.

If the year is divisible by both 4 and 100 then as per rule of leap year we will check if the year is divisible by 400 or not.

If the year is divisible by 400  then we have written a print statement, stating that it is a leap year and in the else section we will print a year that is not a leap year.

Let’s run this program

System will prompt you to enter the year, let’s enter the year 2000. Program will return 2000 is a leap year.

Again run code and enter the year as 2001, program will print 2001 is not a leap year.

Method 2: Pre-defined function to check leap year in Python

Source Code and Output

import calendar

year = 2000
if (calendar.isleap(year)):
    print("Leap Year")
else:
    print("Not a Leap Year")

Output:

Leap Year

Code Explanation Method 2: Pre-defined function to check leap year in Python

In the above code we have used a predefined function of python.

For this we have imported the calendar module of python.

In the next line we have defined a year variable with value 2000, you can even take the value of the year from user input.

Then we have an if condition where we will check if the year is a leap year or not. For this we have used python’s predefined function isleap from calendar module.

The Isleap function accepts only one parameter which is year, and it will return a boolean result.

Inside if we have a print statement, stating that year is a leap year else print, it is not a leap year.

If you run this program then the program will return. It is a leap year because 2000 is a leap year.

Method 3: Check Leap year from Logical operator in a single line in Python

Source Code and Output

year = 2000
if((((year % 4 == 0) and (year % 100 != 0)) or (year % 400 == 0))):
    print("Leap Year")
else:
    print("Not a Leap Year")

Output:

Leap Year

Code Explanation Method 3: Check Leap year from Logical operator in a single line in Python

In the above program we have used the same logic as we have used in the first program, but here we have saved some lines of code, let’s see how.

In the first line we have stored the year into a variable called year. 

And in the next line we have our logic using the logical operator.

If the statement we have checked is that the year is divisible by 4 and not by 100 or the year is divisible by 400, then it is a leap year. Else it is not a leap year.

Run the program and you will get a leap year.

I am sure, you know what is leap year and why it is important.

Facebook Comments
(Visited 1 times, 1 visits today)

Leave A Comment

Your email address will not be published. Required fields are marked *