from datetime import datetime, timedelta a = datetime.strptime('20150101', '%Y%m%d') b = datetime.strptime('20151231', '%Y%m%d') count = 0 while a <= b: d = [int(i) for i in str(datetime.strftime(a, '%m%d'))] if sum(d[:2]) == sum(d[2:]): count += 1 a += timedelta(days=1) print(count)