Python | Print number of leap years from given list of years
Last Updated :
09 May, 2023
The problem of finding leap years is quite generic and we might face the issue of finding the number of leap years in the given list of years. Let’s discuss certain ways in which this can be performed in Python.
Rules to Check a Leap Years in Python
To check if a number is a Leap year or not, two conditions must be satisfied. They are as follows:
- The number should be a multiple of 400
- The number should be a multiple of 4 and not a multiple of 100.
Python Program to Count Number of Leap Years
In Python, there are various methods by which we can count the number of Leap years in a Python List. The main logic is to pass a list of years to a function that checks if a year is a leap year only when the two conditions are satisfied. Let us see a few methods to check this.
Count Number of Leap Years using Iteration
An iterator in Python is an object that is used to iterate over iterable objects like lists, tuples, dictionaries, and sets. Using this method, we will iterate over a list of years.
Example:
In this example, each element of the Python list is passed to the ‘checkYear()’ function. If the function returns True, that means the year passed to the function is a leap year and the ‘Answer’ counter is incremented by 1.
Python3
Input = [ 2001 , 2002 , 2003 , 2004 , 2005 , 2006 ,
2007 , 2008 , 2009 , 2010 , 2011 , 2012 ]
def checkYear(year):
return (((year % 4 = = 0 ) and
(year % 100 ! = 0 )) or
(year % 400 = = 0 ))
Answer = 0
for elem in Input :
if checkYear(elem):
Answer = Answer + 1
print ( "No of leap years are:" , Answer)
|
Output:
No of leap years are: 3
Count Number of Leap Years using Python Calendar
Another method to calculate the number of leap years in a list is by using the Python Calendar module. It is an inbuilt module in Python that provides various calendar-related functionalities.
Example:
In this example, we are using the isleap() function of the calendar module to check if a number passes to it as a leap year or not. If the isleap() functions returns the True value, it means the number is a leap year.
Python3
import calendar
Input = [ 2001 , 2002 , 2003 , 2004 , 2005 ,
2006 , 2007 , 2008 , 2009 , 2010 ]
def FindLeapYear( Input ):
ans = 0
for elem in Input :
if calendar.isleap( int (elem)):
ans = ans + 1
return ans
Output = FindLeapYear( Input )
print ( "No of leap years are:" , Output)
|
Output:
No of leap years are: 2
Count Number of Leap Years using Python Range()
One very common method to calculate the number of leap years in a list is by using loops in Python. The range() function returns a sequence of numbers, in a given range.
Example:
In this method, we will use Python’s range() function and provide the list’s start and end index as the parameter.
Python3
Input = [ 2001 , 2002 , 2003 , 2004 , 2005 , 2006 ,
2007 , 2008 , 2009 , 2010 , 2011 , 2012 ]
def checkYear(year):
return (((year % 4 = = 0 ) and
(year % 100 ! = 0 )) or
(year % 400 = = 0 ))
Answer = 0
for i in range ( 0 , len ( Input )):
if checkYear( Input [i]):
Answer = Answer + 1
print ( "No of leap years are:" , Answer)
|
Output:
No of leap years are: 3
Count the Number of Leap Years using List Comprehension
Python list comprehension is a technique that executes each element of the list with a given expression within a list using a for loop.
Example:
In this example, we are using Python list comprehension to create a list of all the leap years in the input list, and then finding the length of that list.
Python3
Input = [ 2001 , 2002 , 2003 , 2004 , 2005 , 2006 ,
2007 , 2008 , 2009 , 2010 , 2011 , 2012 ]
leap_years = [year for year in Input if year %
4 = = 0 and (year % 100 ! = 0 or year % 400 = = 0 )]
Answer = len (leap_years)
print ( "No of leap years are:" , Answer)
|
Output:
No of leap years are: 3
Related Articles: Check if a given Year is Leap Year
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...