Write a Python program to select all the Sundays of a specified year


The program prints all the Sundays in the year 2023. Here's how it works:

  • The program starts by defining a year variable as a string '2023'.
  • It then loops through 53 weeks of the year using a for loop.
  • Inside the loop, it converts each week into a datetime object using the strptime() method.
  • The strptime() method takes a string and a format code as arguments and returns a datetime object. In this case, the format code '%Y%W%w' is used to convert the year, week number and day of the week into a datetime object.
  • The day of the week is set to 0 (Sunday) in the format code, so the datetime object returned by strptime() will always represent a Sunday.
  • The datetime object is then printed using the print() function.

Source Code

import datetime
year = '2023'
for week in range(53):
    print(datetime.datetime.strptime(year+str(week)+'0', '%Y%W%w'))

Output

2023-01-01 00:00:00
2023-01-08 00:00:00
2023-01-15 00:00:00
2023-01-22 00:00:00
2023-01-29 00:00:00
2023-02-05 00:00:00
2023-02-12 00:00:00
2023-02-19 00:00:00
2023-02-26 00:00:00
2023-03-05 00:00:00
2023-03-12 00:00:00
2023-03-19 00:00:00
2023-03-26 00:00:00
2023-04-02 00:00:00
2023-04-09 00:00:00
2023-04-16 00:00:00
2023-04-23 00:00:00
2023-04-30 00:00:00
2023-05-07 00:00:00
2023-05-14 00:00:00
2023-05-21 00:00:00
2023-05-28 00:00:00
2023-06-04 00:00:00
2023-06-11 00:00:00
2023-06-18 00:00:00
2023-06-25 00:00:00
2023-07-02 00:00:00
2023-07-09 00:00:00
2023-07-16 00:00:00
2023-07-23 00:00:00
2023-07-30 00:00:00
2023-08-06 00:00:00
2023-08-13 00:00:00
2023-08-20 00:00:00
2023-08-27 00:00:00
2023-09-03 00:00:00
2023-09-10 00:00:00
2023-09-17 00:00:00
2023-09-24 00:00:00
2023-10-01 00:00:00
2023-10-08 00:00:00
2023-10-15 00:00:00
2023-10-22 00:00:00
2023-10-29 00:00:00
2023-11-05 00:00:00
2023-11-12 00:00:00
2023-11-19 00:00:00
2023-11-26 00:00:00
2023-12-03 00:00:00
2023-12-10 00:00:00
2023-12-17 00:00:00
2023-12-24 00:00:00
2023-12-31 00:00:00

Example Programs