Check Leap year from Logical operator in a single line in Python

Check Leap year from Logical operator in a single line in Python
(Last Updated On: 11/10/2022)

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.

Check Leap Year in Python Program

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.

Program to Check Leap year from Logical operator in a single line in Python

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 :

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.

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.

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

Leave a Reply

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