Python | Print number of leap years from given list of years

The problem of finding leap year is quite generic and we might face the issue of finding the number of leap years in given list of years. Let’s discuss certain ways in which this can be performed.

Method #1: Using Iteration

Check whether year is a multiple of 4 and not multiple of 100 or year is multiple of 400.



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to finding number of 
# leap years in list of years.
  
# Input list initialization
Input = [2001, 2002, 2003, 2004, 2005, 2006,
         2007, 2008, 2009, 2010, 2011, 2012]
  
# Find whether it is leap year or not
def checkYear(year):
    return (((year % 4 == 0) and
             (year % 100 != 0)) or
             (year % 400 == 0)) 
   
# Answer Initialization
Answer = 0 
  
for elem in Input:
    if checkYear(elem):
        Answer = Answer + 1
  
# Printing
print("No of leap years are:", Answer)

chevron_right


Output:

No of leap years are: 3

 
Method #2: Using calender

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to finding number of 
# leap years in list of years.
  
# Importing calender
import calendar
  
# Input list initialization
Input = [2001, 2002, 2003, 2004, 2005,
         2006, 2007, 2008, 2009, 2010]
  
# Using calender to find leap year
def FindLeapYear(Input):
    ans = 0
    for elem in Input:
        if calendar.isleap(int(elem)):
            ans = ans + 1
    return ans
  
Output = FindLeapYear(Input)
  
# Printing
print("No of leap years are:", Output)

chevron_right


Output:

No of leap years are: 2


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.