import datetime dt = datetime.date(2015, 1, 1) cnt = 0 for i in range(365): cdt = dt + datetime.timedelta(days=i) if cdt.month == sum(map(int, str(cdt.day))): cnt += 1 print(cnt)