Leap year in python assignment expert

Leap Year

Write a program to determine whether the given year Y is a leap year or not. A normal year consists of 365 days. But the time required for Earth to revolve around the Sun is around 365.2425 days. So a "leap year" of 366 days is used once every four years to eliminate the error caused by three normal (but short) years. Any year that is divisible by 4 is usually a leap year: for example, 1988, 1992, and 1996 are leap years.

However, there is still a small error that must be accounted for. To eliminate this error, the calendar considers that a year that is divisible by 100 (for example, 1900) is a leap year only if it is also divisible by 400. For this reason, the following years are not leap years: 1700, 1800, 1900, 2100, 2200, 2300, 2500, 2600, ... This is because they are divisible by 100 but not by 400.

The following years are leap years: 1600, 2000, 2400 This is because they are divisible by both 100 and 400.

Leap Year

This Program name is Leap Year. Write a Python program to Leap Year, it has two test cases

The below link contains Leap Year question, explanation and test cases

https://drive.google.com/file/d/1OxFAPhxSfay0CussSEZ_OmW78GAHhOvk/view?usp=sharing

We need exact output when the code was run

def LeapYear(n):
    if (n % 4) == 0:
        if (n % 100) == 0:
            if (n % 400) == 0:
                print("True")
            else:
                print("False\n")
        else:
            print("True")
    else:
        print("False\n")
    


print("Test Case – 1")
year = int(input("Input\n"))
print("Output")
LeapYear(year)




print("\nTest Case – 2")
year1 = int(input("Input\n"))
print("Output")
LeapYear(year1)
    

Learn more about our help with Assignments: Python

Leap year in python assignment expert

Introduction to Leap Year Program in Python

A year is considered a leap year if that year is exactly divisible by 4, except for years that end with 00(century year). A century year is a leap year if that is exactly divisible by 400. However, a year which not divisible by 400 and divisible by 100 is not a leap year.

Example:

  • 2004 is a leap year.
  • 2020 is a leap year.
  • 1900 is not a leap year.
  • 2013 is not a leap year.

Examples of Leap Year in Python

Below are the different examples of leap year in python:

Example #1 – Using Elif

Code:

input_year = int(input("Enter the Year to be checked: "))
if (input_year%400 == 0):
          print("%d is a Leap Year" %input_year)
elif (input_year%100 == 0):
          print("%d is Not the Leap Year" %input_year)
elif (input_year%4 == 0):
          print("%d is a Leap Year" %input_year)
else:
          print("%d is Not the Leap Year" %input_year)

Output:

Leap year in python assignment expert

Explanation:

  • First, the user will enter the year of his choice, which needs to be checked for leap year. As per the algorithm, if the year divisible by 400, that year is straightway leap year. And so the first if statement is “(input_year%400 == 0)”.
  • Now when the year not divisible by 400, the second If statement will be executed that is “(input_year%100 == 0)”. If “(input_year%100 == 0)” evaluates to True. That means it’s a century year but not a leap year.
  • Now when both the above statement doesn’t satisfy to True, then the third statement will be evaluated “input_year%4 == 0”. And if this satisfies, then that year is a leap year, Else not a leap year.

Example #2 – Using Nested If

Code:

input_year = int(input("Enter the Year to be checked: "))
if(input_year%4 == 0):
    if(input_year%100 == 0):
        if(input_year%400 == 0):
            print("%d is Leap Year" %input_year)
        else:
            print("%d is Not the Leap Year" %input_year)
    else:
        print("%d is Leap Year" %input_year)
else:
    print("%d is Not the Leap Year" %input_year)

Output:

Leap year in python assignment expert

Explanation:

  • One needs to enter the year he/she wants to check for leap year. First condition “(input_year%4 == 0)” will checked. If it comes true, then it will be inside the nested if-else statement. Else If condition “(input_year%4 == 0)” evaluates to False, then straightway that year is not a leap year.
  • Now “(input_year%4 == 0)” gets evaluated. Condition “(input_year%4 == 0)” is checked for century year. When condition “(input_year%100 == 0)” evaluates to False i.e. remainder comes means that year is not century year, and then that is a leap year.
  • Condition “(input_year%100 == 0)” when evaluates to True i.e. remainder is zero.Then it goes the further nested if statement for checking the divisibility with 400. Now “(input_year%400 == 0)” gets evaluated. When the year is divisible by 400, that year is a leap year. Else, not a leap year.

Example #3 – Using Conditional Statement

Code:

input_year = int(input("Enter the Year to be checked: "))
if (( input_year%400 == 0)or (( input_year%4 == 0 ) and ( input_year%100 != 0))):
    print("%d is Leap Year" %input_year)
else:
    print("%d is Not the Leap Year" %input_year)

Output:

Leap year in python assignment expert

Explanation:

  • Enter year, one wishes to check for leap/ non-leap year. Condition (input_year%400 == 0) OR That means any year which is exactly divisible by 400, is undoubtedly leap year. Condition (( input_year%4 == 0 ) AND ( input_year%100 != 0)). Here it comes from logic that a year divisible by 4, and not divisible by 100 is a leap year.
  • First the condition “( input_year%4 == 0 )” will be evaluated. When it results in True, then condition “( input_year%100 != 0 ) “ will be evaluated. Condition “( input_year%100 != 0 ) “ is to evaluate whether year is century year or not. So, the inference can be made that a Non-century year is a leap year if it’s divisible by 4.

Here are the conditional table of “Or” and “And” to be referred to.

OR:

Leap year in python assignment expert

AND:

Leap year in python assignment expert

Then the result will be printed accordingly.

Example #4 – Using Function

Code:

#function defined 
def LeapYearCheck(input_year): 
    if (input_year % 4) == 0: 
        if (input_year % 100) == 0: 
            if (input_year % 400) == 0: 
                return True
            else: 
                return False
        else: 
             return True
    else: 
        return False  
# Driver Code  
input_year = int(input("Enter the Year to be checked: "))
if(LeapYearCheck(input_year)): 
    print("Leap Year") 
else: 
    print("Not a Leap Year")

Output:

Leap year in python assignment expert

Explanation:

  • This approach is similar to approach 2; it’s just the steps of approach 2(referred to above) is enclosed in the form of function. The function is called every time the year needs to be checked for leap/ non-leap year.
  • Writing function for any task is a good practice in python. As it helps in the modularity of code and hence enhances the readability of code.

Conclusion

Here we saw the logic of finding leap year and its various ways of implementation in Python. Here we have listed four main approaches; one can try on many other approaches as well. There are always many ways of doing the same thing. Here it’s just checking of year, so the speed is not much of matter. However, if logic is complex, speed does matter a lot.

This is a guide to Leap Year Program in Python. Here we discuss the top examples of the Leap Year Program in Python and its code implementation. You can also go through our other related articles to learn more –

  1. Substrings in Python
  2. String Array in Python
  3. Python Global Variable
  4. Leap Year Program in Java

How do you code a leap year in Python?

Example:.
# Default function to implement conditions to check leap year..
def CheckLeap(Year):.
# Checking if the given year is leap year..
if((Year % 400 == 0) or..
(Year % 100 != 0) and..
(Year % 4 == 0)):.
print("Given Year is a leap Year");.
# Else it is not a leap year..

How do you code a leap year?

Any year that is evenly divisible by 4 is a leap year: for example, 1988, 1992, and 1996 are leap years. ... Formula to determine whether a year is a leap year..

How does Python calculate leap year between two years?

start = int(input("Enter start year: ")) end = int(input("Enter end year: ")) if start <= end: leap_years = [str(x + start) for x in range(end-start) if x % 4 == 0 and x % 100 != 0] leap_years[-1] += "." print(f"Here is a list of leap years between {start} and {end}:\n{(', '.

Is 2100 a leap year?

The rule is that if the year is divisible by 100 and not divisible by 400, leap year is skipped. The year 2000 was a leap year, for example, but the years 1700, 1800, and 1900 were not. The next time a leap year will be skipped is the year 2100.