How to find all Sundays of a calendar year in python

In this tutorial, We will see How to find all Sundays of a calendar year in python. So in order to find all Sundays in the given calendar year, we are going to use calendar and datetime module in our python program. Before going to write a program we need to understand calendar and datetime module.

What is calendar module in python?

Calendar module is inbuilt module in python which provide useful function to solve task based on date ,month and year. By default,Monday taken at index [0] ( First day of the week ) and Sunday at [6]( last day of week ).

What is datetime module in python?

Python has also inbuilt module datetime which allow us to solve various task on date and time.

Program to find all Sundays of a calendar year in Python

So, let’s start to understand the program in an easily way



from datetime import date
import calendar

Here, we include the date from datetime module also include the calendar module in the program .

Year=2019
A=calendar.TextCalendar(calendar.SUNDAY)

We assume the year provided from the user is 2019 and by using TextCalendar  we generate the plain text calendar.

for b in range(1,13):
    for k in A.itermonthdays(Year,b):
        if k!=0:
            day=date(Year,b,k)
            if day.weekday()==6:
                print("%s,%d-%d-%d" % (calendar.day_name[6] ,k,b,Year))

In this code, we use range from 1 to 13 because the no of month in a year is 12 and also if the value of k is equal to zero that means day of the week are in next month. As  i said above index of Sunday by default equal to 6 so if index of day equal to 6 then the day of week will be Sunday and print it with date of the calendar year.

Sunday of a calendar year with date

Python program:

from datetime import date
import calendar
Year= 2019
A=calendar.TextCalendar(calendar.SUNDAY)
for b in range(1,13):
    for k in A.itermonthdays(Year,b):
        if k!=0:
            day=date(Year,b,k)
            if day.weekday()==6:
                print("%s,%d-%d-%d" % (calendar.day_name[6] ,k,b,Year))

Output:

Sunday,6-1-2019
Sunday,13-1-2019
Sunday,20-1-2019
Sunday,27-1-2019
Sunday,3-2-2019
Sunday,10-2-2019
Sunday,17-2-2019
Sunday,24-2-2019
Sunday,3-3-2019
Sunday,10-3-2019
Sunday,17-3-2019
Sunday,24-3-2019
Sunday,31-3-2019
Sunday,7-4-2019
Sunday,14-4-2019
Sunday,21-4-2019
Sunday,28-4-2019
Sunday,5-5-2019
Sunday,12-5-2019
Sunday,19-5-2019
Sunday,26-5-2019
Sunday,2-6-2019
Sunday,9-6-2019
Sunday,16-6-2019
Sunday,23-6-2019
Sunday,30-6-2019
Sunday,7-7-2019
Sunday,14-7-2019
Sunday,21-7-2019
Sunday,28-7-2019
Sunday,4-8-2019
Sunday,11-8-2019
Sunday,18-8-2019
Sunday,25-8-2019
Sunday,1-9-2019
Sunday,8-9-2019
Sunday,15-9-2019
Sunday,22-9-2019
Sunday,29-9-2019
Sunday,6-10-2019
Sunday,13-10-2019
Sunday,20-10-2019
Sunday,27-10-2019
Sunday,3-11-2019
Sunday,10-11-2019
Sunday,17-11-2019
Sunday,24-11-2019
Sunday,1-12-2019
Sunday,8-12-2019
Sunday,15-12-2019
Sunday,22-12-2019
Sunday,29-12-2019

So Guy’s, I hope you enjoy this tutorial.

You may also read:

Leave a Reply

Your email address will not be published. Required fields are marked *