from datetime import date, timedelta now = date(2015, 1, 1) td = timedelta(days=1) fin = date(2016, 1, 1) ans = 0 while now < fin: m, d = now.month, now.day dd = str(d) + "0" ddd = int(dd[0]) + int(dd[1]) if m == ddd: ans += 1 now += td print(ans)