import datetime as dt from dateutil.relativedelta import relativedelta as r d = dt.datetime(2015, 1, 1) e = d + r(years=1) hd = 0 while d < e: if d.month == sum([int(n) for n in list(str(d.day))]): hd += 1 d += r(days=1) print(hd)