Leap year check-in Python: Here, we will figure out how to check a given year is a leap year or not in Python programming language?
A leap year is a year that is totally distinct by 4 aside from the century year (a year that finished with 00).
A century year is a leap year on the off chance that it is distinct by 400. Here, a year is given by the client and we need to check whether the given year is a leap year or not.
This issue, we will explain in two different ways first by utilizing the schedule module and second by basically checking the leap year condition.
1) By utilizing the schedule module
Prior to going to take care of the issue, at first, we gain proficiency with a tad about the schedule module.
Schedule module is inbuilt in Python which gives us different capacities to tackle the issue identified with date, month and year.
Program:
# importing the module
import calendar
# input the year
year=int(input('Enter the value of year: '))
leap_year=calendar.isleap(year)
# checking leap year
if leap_year: # to check condition
print('The given year is a leap year.')
else:
print('The given year is a non-leap year.')
Output:
RUN 1:
Enter the value of year: 2020
The given year is a leap year.
RUN 2:
Enter the value of year: 2021
The given year is a non-leap year.
2) By basically checking the leap year condition
As we probably are aware of the condition to check the given year is a leap year or not. Thus, here
we will actualize the condition and attempt to compose the Python program.
Program:
# input the year
y=int(input('Enter the value of year: '))
# To check for non century year
if y%400==0 or y%4==0 and y%100!=0:
print('The given year is a leap year.')
else:
print('The given year is a non-leap year.')
Output:
RUN 1:
Enter the value of year: 2020
The given year is a leap year.
RUN 2:
Enter the value of year: 2000
The given year is a leap year.