from datetime import date, timedelta now = date(2015,1,1) end = date(2015,12,31) day_inc = timedelta(days=1) result = 0 def split_num(n): if n < 10: return 0, n else: string = str(n) return int(string[0]), int(string[1]) while now <= end: m = now.month now_day = now.day d1, d2 = split_num(now_day) if m == d1 + d2: result += 1 now += day_inc print(result)