VOOZH about

URL: https://www.geeksforgeeks.org/python/python-program-to-find-the-first-day-of-given-year/

⇱ Python program to find the first day of given year - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python program to find the first day of given year

Last Updated : 26 Mar, 2023

Given a year as input, write a Python to find the starting day of the given year. We will use the Python datetime library in order to solve this problem. 

Example: 

Input : 2010
Output :Friday
Input :2019
Output :Tuesday

Below is the implementation: 


Output
Starting day of 2010 is Friday.

Time Complexity: O(1)

Space Complexity: O(1)

Alternate Approach Using the calendar library

step by step approach 

  1. Import the calendar library.
  2. Define a function Startingday(year) that takes a single argument year.
  3. Create a list called Days that contains the names of the days of the week in order, starting with
  4. Monday and ending with Sunday.
  5. Call the calendar.weekday(year, 1, 1) function, passing in the year argument and the values 1 and 1 for the month and day arguments. This function returns an integer representing the day of the week for the given date, where Monday is 0 and Sunday is 6.
  6. Use the integer returned by calendar.weekday() to index into the Days list and get the name of the starting day of the year.
  7. Print a message that includes the year argument and the name of the starting day of the year.
  8. Define a variable year with the value 2019.
  9. Call the Startingday(year) function, passing in the year argument.
  10. The program outputs a message indicating the starting day of the year 2019, which is a Tuesday.
  11. The author of the code is credited in a comment at the end of the program.

Output
Starting day of 2019 is Tuesday.

Time Complexity: O(1)

Auxiliary Space: O(1)

Comment