How to Find the Number of Days Occurs in a Year using Python

In this tutorial, we will program 'How to Find the Number of Days in a Year.' We will learn how to calculate the total number of days in a given year. The objective is to determine all the possible days in a year, accounting for leap years as well. I will provide a sample program to demonstrate the actual coding process in this tutorial.

This topic is very easy to understand. Just follow the instructions I provide, and you will be able to do it yourself with ease. The program I will show you covers the basics of programming for calculating all the possible days in a year. I will do my best to provide you with a simple method for listing the total number of days in a year. So, let's start coding.

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. import datetime
  2. import calendar
  3.  
  4. def day_occur_time(year):
  5.        
  6.        
  7.         days = [ "Monday", "Tuesday", "Wednesday",
  8.                 "Thursday", "Friday", "Saturday",
  9.                 "Sunday" ]
  10.        
  11.        
  12.         L = [52 for i in range(7)]
  13.        
  14.        
  15.         pos = -1
  16.         day = datetime.datetime(year, month = 1, day = 1).strftime("%A")
  17.         for i in range(7):
  18.                 if day == days[i]:
  19.                         pos = i
  20.                        
  21.        
  22.         if calendar.isleap(year):
  23.                 L[pos] += 1
  24.                 L[(pos+1)%7] += 1
  25.                
  26.         else:
  27.                 L[pos] += 1
  28.                
  29.        
  30.         for i in range(7):
  31.                 print(days[i], L[i])
  32.        
  33.  
  34. ret = False
  35.  
  36.  
  37. while True:
  38.         print("\n================= Number of Days Occurs in a Year =================\n\n")
  39.        
  40.         year = int(input("Enter Year: "))
  41.         day_occur_time(year)
  42.         opt = input("\nDo you want to try again?(yes/no): ")
  43.  
  44.         if opt.lower() == 'yes':
  45.                 ret=False
  46.         elif opt.lower() == 'no':
  47.                 ret=True
  48.                 print("Exiting program....")
  49.  
  50.         else:
  51.                 print("Please enter yes/no:")
  52.                 break
  53.  
  54.         if ret == False:
  55.                 continue

This Python script calculates and displays how many times each day of the week occurs in a given year. It takes the year as input and determines the day of the week for January 1st of that year. The script then calculates the number of occurrences for each day of the week, accounting for whether the year is a leap year or not. The user can repeatedly check different years until they choose to exit the program.

Output:

There you have it we successfully created How to Find the Number of Days Occurs in a Year using Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment