from datetime import date from dateutil.relativedelta import relativedelta dt = date(2016,1,1) happy = 0 for i in range(1,366): cl = dt-relativedelta(days=i) chk = str(cl.day) if len(str(chk)) == 2: chk = int(str(cl.day)[0] + str(cl.day)[1]) if cl.month == chk: happy +=1 print(happy)