from datetime import datetime from datetime import timedelta start_day = datetime.strptime('20150101','%Y%m%d') end_day = datetime.strptime('20151231','%Y%m%d') def daterange(start,end): for n in range((end_day - start_day).days+1): yield start + timedelta(n) a = [] b = [] for i in daterange(start_day,end_day): a.append(i.month) b.append(str(i.day)) c=0 for i,j in zip(a,b): d = 0 for k in j: d += int(k) print(k,d) if d == i: c+=1 print(c)