I am trying to figure out what day of the week the leap day falls under (ex. Sunday, Monday, etc) for each year (ex. 1972 Tuesday) in a range of years.
The code below checks that each year is a leap year and appends it to an array:
import array as year
LeapYear = []
startYear = 1970
endYear = 1980
for year in range(startYear, endYear):
if (0 == year % 4) and (0 != year % 100) or (0 == year % 400):
LeapYear.append(year)
print(year)
>Solution :
You are kind of reinventing the wheel. You can use calendar.isleap to determine if a year is a leap year. To get the zero-indexed day of the week (with the week starting on Mondays), you can use calendar.weekday.
import calendar
days = 'Mon Tue Wed Thu Fri Sat Sun'.split()
start = 1994
end = 2025
for year in range(start, end):
if calendar.isleap(year):
day_ix = calendar.weekday(year, 2, 29)
print(year, days[day_ix])
# prints:
1996 Thu
2000 Tue
2004 Sun
2008 Fri
2012 Wed
2016 Mon
2020 Sat
2024 Thu