Skip to content
Advertisement

Return list of True/ False bool value for list of leap years given as an argument

I have to check whether year = [1900, 2020, 2021, 2001] is a leap year or not and get True/False as a result (Boolean)

I have written a function as :

def is_leap_year(year):
   return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)

But now I need to pass a list as year = [1900, 2020, 2021, 2001] and when I am doing so I am getting an error as “TypeError: unsupported operand type(s) for %: ‘list’ and ‘int'”

How do I pass a list as an argument in a function?

Advertisement

Answer

You can simply do this:

def is_leap_year(year):
   return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
years = [1900, 2020, 2021, 2001]
print(list(map(is_leap_year,years)))

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement