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.
# Python code to finding number of # leap years in list of years. # Input list initializationInput = [2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012] # Find whether it is leap year or notdef checkYear(year): return (((year % 4 == 0) and (year % 100 != 0)) or (year % 400 == 0)) # Answer InitializationAnswer = 0 for elem in Input: if checkYear(elem): Answer = Answer + 1 # Printingprint("No of leap years are:", Answer) |
Output:
No of leap years are: 3
Method #2: Using calender
# Python code to finding number of # leap years in list of years. # Importing calenderimport calendar # Input list initializationInput = [2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010] # Using calender to find leap yeardef FindLeapYear(Input): ans = 0 for elem in Input: if calendar.isleap(int(elem)): ans = ans + 1 return ans Output = FindLeapYear(Input) # Printingprint("No of leap years are:", Output) |
Output:
No of leap years are: 2
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.


