How do you find if a year is a leap year in python?

Python Program to Check Leap Year

Leap Year:

A year is called a leap year if it contains an additional day which makes the number of the days in that year is 366. This additional day is added in February which makes it 29 days long.

A leap year occurred once every 4 years.

How to determine if a year is a leap year?

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 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 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.

Look at the following program to understand the implementation of it:

Example:

Output:

Enter the number: 1700
Given year is not a leap Year

Explanation:

We have implemented all the three mandatory conditions (that we listed above) in the program by using the 'and' and 'or' keyword inside the if else condition. After getting input from the user, the program first will go to the input part and check if the given year is a leap year. If the condition satisfies, the program will print 'Leap Year'; else program will print 'Not a leap year.'



How do you find if a year is a leap year in python?
For Videos Join Our Youtube Channel: Join Now


Feedback

  • Send your Feedback to [email protected]

Help Others, Please Share

How do you find if a year is a leap year in python?
How do you find if a year is a leap year in python?
How do you find if a year is a leap year in python?






A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400. For example,

2017 is not a leap year
1900 is a not leap year
2012 is a leap year
2000 is a leap year

Source Code

# Python program to check if year is a leap year or not

year = 2000

# To get year (integer input) from the user
# year = int(input("Enter a year: "))

# divided by 100 means century year (ending with 00)
# century year divided by 400 is leap year
if (year % 400 == 0) and (year % 100 == 0):
    print("{0} is a leap year".format(year))

# not divided by 100 means not a century year
# year divided by 4 is a leap year
elif (year % 4 ==0) and (year % 100 != 0):
    print("{0} is a leap year".format(year))

# if not divided by both 400 (century year) and 4 (not century year)
# year is not leap year
else:
    print("{0} is not a leap year".format(year))

Output

2000 is a leap year

You can change the value of year in the source code and run it again to test this program.

An alternative one liner:

((((y % 4) + (int((y - (y % 100)) / y) * ((y % 400) / 100))) - 1) < 0)

This was something that I put together for fun (?) that is also 1:1 compatible with C.

(y % 4) >>>It first checks if the year is a leap year via the typical mod-4 check.

(int((y - (y % 100)) / y) >>>It then accounts for those years divisible by 100. If the year is evenly divisible by 100, this will result in a value of 1, otherwise it will result in a value of 0.

((y % 400) / 100))) >>>Next, the year is divided by 400 (and subsequently 100, to return 1, 2, or 3 if it is not.

These two values

(int(y - (y % 100)) / y)

&

((y % 400) / 100)))

are then multiplied together. If the year is not divisible by 100, this will always equal 0, otherwise if it is divisible by 100, but not by 400, it will result in 1, 2, or 3. If it is divisible by both 100 and 400, it will result in 0.

This value is added to (y % 4), which will only be equal to 0 if the year is a leap year after accounting for the edge-cases.

Finally, 1 is subtracted from this remaining value, resulting in -1 if the year is a leap year, and either 0, 1, or 2 if it is not. This value is compared against 0 with the less-than operator. If the year is a leap year this will result in True (or 1, if used in C), otherwise it will return False (or 0, if used in C).

Please note: this code is horribly inefficient, incredibly unreadable, and a detriment to any code attempting to follow proper practices. This was an exercise of mine to see if I could do so, and nothing more.

Further, be aware that ZeroDivisionErrors are a consequence of the input year equaling 0, and must be accounted for.

For example, a VERY basic timeit comparison of 1000 executions shows that, when compared against an equivalent codeblock utilizing simple if-statements and the modulus operator, this one-liner is roughly 5 times slower than its if-block equivalent.

That being said, I do find it highly entertaining!

How do you calculate if a year is a leap year Python?

If a year is divisible by 100, but not by 400..
# 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 check if a year is a leap year?

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