We add a Leap Day on February 29, almost every four years. The leap day is an extra, or intercalary day and we add it to the shortest month of the year, February.
In the Gregorian calendar three criteria must be taken into account to identify leap years:
[1] The year can be evenly divided by 4, is a leap year, unless:
[2] The
year can be evenly divided by 100, it is NOT a leap year, unless:
[3] The year is also evenly divisible by 400, Then it is a leap year.
This means that in the Gregorian calendar, the years 2000 and 2400 are leap years, while 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years.
Below is the python code to satisfy above three conditions hence finding whether an inputted year is leap year or not.
# # Program to confirm is a given year is leap year or not # year is provided at runtime. # Author: //www.pythoncircle.com # def is_leap[year]: leap = False if year % 4 == 0: if year % 100 == 0: if year % 400 == 0: return True return False return True else: return False return leap if __name__ == '__main__': print[is_leap[int[input[]]]]
Copy the above code in a file and save it with the name leap_year.py
and run using the command python3 leap_year.py
.
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.
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.
- 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.
- 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.
- 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.'
Feedback
- Send your Feedback to [email protected]