from datetime import datetime, timedelta cur = datetime(2015, 1, 1) end = datetime(2016, 1, 1) count = 0 while cur < end: x = cur.month y = sum([int(c) for c in str(cur.day)]) if x == y: count += 1 cur += timedelta(days=1) print count