Days in month function using Python

Posted by Samath
  1920
March 25, 2015

 

Write a function days_in_month which takes a month as an argument and finds the corresponding month in the month_days list and returns the number of days associated with that month.

>>> days_in_month('December')
[31]
>>> days_in_month('February')
[28, 29]

Code:

def days_in_month(month):
    month_days=[('January',[31]),('February',[28,29]),('March',[31]),('April',[30]),('May',[31]),('June',[30]),('July',[31]),('August',[31]),('September',[30]),('October',[31]),('November',[30]),('December',[31])]
    for i in month_days:
        if month==i[0]:
            return i[1]