Python program to check leap year
In this article, you will learn different ways in Python programming to check whether a particular year is a leap year or not. A leap year is 366 days old rather than 365 days, in light of the fact that February is about 29 days long instead of the normal 28 days. This additional day occurs in years which are multiples of four, i.e., 1992, 1996, 2000, 2004, 2008, 2012.
Logic for the Leap Year
Most of us definitely know the logic behind checking for a leap year in a Gregorian calendar. These are the three rules that should be followed to identify a leap year.
- The year can be evenly divided by 4,
- If the year can be evenly divided by 100, it is NOT a leap year, except if;
- The year is also evenly divisible by 400. Then it is a leap year.
Python program to check leap year
In the given Python program, the user is asked to enter a year. The program checks whether the entered year is a leap year or not.
# Python program to check leap year
# getting user input
year = int(input("Enter a year: "))
# leap year if the year is evenly divided by 4
if (year % 4) == 0:
# leap year if the year is evenly divided by 100
if (year % 100) == 0:
# leap year if evenly divisible by 400
if (year % 400) == 0:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
else:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
Output1:
Enter a year: 2020
2020 is a leap year
Output2:
Enter a year: 2005
2005 is not a leap year
Output3:
Enter a year: 2010
2010 is not a leap year
Python program to check leap year between given range
In the given Python program, we will find the leap years between the given range of two years.
# Python program to check leap year
# getting user inputs
print ("Enter the lowest year:")
startYear = int(input())
print ("Enter the highest year: ")
endYear = int(input())
print ("Leap years in given range are: ")
#loop between the start and end year
for year in range(startYear, endYear):
#check for leap year
if (0 == year % 4) and (0 != year % 100) or (0 == year % 400):
print (year)
Output1:
Enter the lowest year:
1990
Enter the highest year:
1998
Leap years in given range are:
1992
1996
Output2:
Enter the lowest year:
2010
Enter the highest year:
2020
Leap years in given range are:
2012
2016
Related Articles
Python program to find the median of three values
Matrix subtraction in Python
Age calculator in Python
Permutation of string in Python
Pascal triangle pattern in Python
Sort list in descending order Python
Cube root in Python
Bubble sort program in Python
Pandas string to datetime
Convert Excel to CSV Python Pandas
Selection sort program in Python
Python Pandas Dataframe to CSV
Merge sort program in Python
Remove element from list Python
Convert speech to text in Python
Python program to sum all the numbers in a list
Python print without newline
Python iterate list with index
Exponential function in Python
Calculate a Percentage in Python
Python dict inside list
Python convert dict to xml
Python weather api
Python raise keyword