I need to write a code to find which month in a year 2018 for example has 5 or more Sundays? I want to use calendar module in Python and save the resulting months in abbreviated form. I saw documentation for calendar module but couldnt figure out the code.
Asked
Active
Viewed 5,363 times
-3
-
Check this out https://stackoverflow.com/questions/2003870/how-can-i-select-all-of-the-sundays-for-a-year-using-python – Vishwas Sep 27 '19 at 21:16
-
1"couldnt figure out the code" [sic] ... ["Can Someone Help Me?" is not a valid SO question](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question). This usually suggests that what you need is time with a local tutor or walk through a tutorial, rather than Stack Overflow. Best of all, each tutorial will teach you a collection of related techniques, rather than merely solving the immediate problem. – Prune Sep 27 '19 at 21:18
5 Answers
1
Here's one way. And also, go back to https://docs.python.org/3/library/calendar.html and try playing around with some of the things described in it.
import calendar
def find_five_sunday_months(year):
calendar.setfirstweekday(calendar.SUNDAY)
five_sunday_months = []
for month in range(1, 13):
calendar_month = calendar.monthcalendar(year, month)
# If you're counting Sunday as the first day of the week, then any month that extends into
# six weeks, or starts on a Sunday and extends into five weeks, will contain five Sundays.
if len(calendar_month) == 6 or (len(calendar_month) == 5 and calendar_month[0][0] == 1):
five_sunday_months.append(calendar.month_abbr[month])
return five_sunday_months
print (find_five_sunday_months(2018))

Tané Tachyon
- 1,092
- 8
- 11
0
Thanks @TaneTachyon for the document link you shared. It helped me lot for exploring the calendar module.
I have come up with below code for the same task.
I know it's a bit long, but for a beginner, logic and learning to use different functions, this helped me a lot:
import calendar
calendar.setfirstweekday(calendar.SUNDAY)
year = 2018
a = ()
Sun_month = []
leap = calendar.isleap(year)
for i in range(1,13):
a = calendar.monthrange(year, i)
#print(a[1])
if a[1] == 30:
if (a[0] == 5 or a[0] == 6):
s = calendar.month_abbr[i]
Sun_month.append(s)
elif a[1] == 31:
if (a[0] == 4 or a[0] == 5 or a[0] == 6):
s = calendar.month_abbr[i]
Sun_month.append(s)
elif leap == True:
if (a[0] == 6):
s = calendar.month_abbr[i]
Sun_month.append(s)
print("The months having >= 5 sundays in {} is".format(year))
print(Sun_month)

SherylHohman
- 16,580
- 17
- 88
- 94

Jenifer J
- 15
- 1
0
import calendar as c
a = []
for i in range(1,13):
a.append(list(c.monthrange(2018,i)))
b = c.month_abbr[1:13]
c = []
for i in range(0,12):
if (a[i][0]>4 and a[i][1]==30) or (a[i][0]>=4 and a[i][1]==31):
c.append(b[i])
print(c)

deadshot
- 8,881
- 4
- 20
- 39
-
1Welcome to Stack Overflow. Code dumps without any explanation are rarely helpful. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please [edit] your question and explain how it works better than what the OP provided. – ChrisGPT was on strike May 14 '20 at 00:34
-1
import calendar
# here all the weeks are set to start from sunday for every month
calendar.setfirstweekday(calendar.SUNDAY)
l = list() # empty list
# the range will be from 1 to 12
for month in range(1, 13):
# for each month in 2018 month calendar is created
calendar_month = calendar.monthcalendar(2018, month)
# to count no.of sundays present in each month
c = 0
# len(calendar_month) for number of rows(i.e. weeks) in the month
for i in range(len(calendar_month)):
# calendar_month[i][0] first day(i.e. sunday) of ith week which is
#sunday if date is greater than 0
if calendar_month[i][0] > 0:
c += 1 # each sunday is counted
if c == 5:
# calendar.month_abbr[month] abbreviated form of month with 5 sundays
#is added to the list
l.append(calendar.month_abbr[month])
print(l)

b.s. Mohan
- 39
- 2
-
1While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Adrian Mole Jun 16 '20 at 10:14
-4
#With Explanation
import calendar
def find_five_sunday_months(year):
calendar.setfirstweekday(calendar.SUNDAY)
five_sunday_months = []
for month in range(1, 13):
calendar_month = calendar.monthcalendar(year, month)
print('--------------------------------------------------------------------------------')
print('month :',calendar.month_name[month])
print('Count returned tuple as no. of weeks in a month :',calendar_month)
print('No. of weeks in a month:',len(calendar_month))
print('if we have 5 weeks and 5 sundays,it should print 1 else 0 :',calendar_month[0][0])
# If you're counting Sunday as the first day of the week, then any month that extends into
# six weeks, or starts on a Sunday and extends into five weeks, will contain five Sundays.
if len(calendar_month) == 6 or (len(calendar_month) == 5 and calendar_month[0][0] == 1):
print('Yay!')
five_sunday_months.append(calendar.month_abbr[month])
return five_sunday_months
print (find_five_sunday_months(2020))
-
could you edit your answer to include some explanation to what you are doing? – jpnadas Jun 26 '20 at 08:40
-
#OUTPUT WILL LOOK LIKE THIS FOR MARCH: -------------------------------------------------------------------------------------------- month : March Count returned tupple as no. of weeks in a month : [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, 21], [22, 23, 24, 25, 26, 27, 28], [29, 30, 31, 0, 0, 0, 0]] No. of weeks in a month: 5 if we have 5 weeks and 5 sundays,it should print 1 else 0 : 1 Yay! – Shivangi Jun 26 '20 at 09:21