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