A classic case of programming in Python [examination questions] determine the date is the day of the year

This article explores classic cases of Python programming with you, allowing you to learn Python immersively. You can think about the topic first, and then compare it with the problem-solving methods in this article. If you have different opinions, welcome to discuss with me in the official account.

1. Classic case [examination question]

  
Question:
  
Enter a date, and determine what day of the year is this day?
  
Input:
  
20220809
  
Output:
221

  
  

2. Classic case problem-solving method

  

1 Method 1: Find the number of days by calculating the time difference between this day and January 1 of the year

  
The first way to solve the problem in the above case is:
  
step1: Intercept the year of the input time, and piece together January 1 of that year as the reference date.
  
Step2: Calculate the interval between the input date and the reference date, add 1 to the interval to find the day of the year.
  
The specific code is as follows:

from datetime import date

raw_date = input('请按yyyymmdd方式输入年月日')
def y_days1(raw_date):
    date1 = date(int(raw_date[0:4]), 1, 1)
    date2 = date(int(raw_date[0:4]), int(raw_date[4:6]), int(raw_date[6:8]))
    return (date2 - date1).days + 1
y_days1(raw_date)

input('Please enter the year, month, and day according to yyyymmdd'): Manually enter the date.
  
raw_date[0:4]: Intercept the first to fourth digits of the string.
  
If the date is manually entered as 20220105, the result is:

5

You can manually verify that the result is correct. The advantage of this method is that the logic is simple and clearer to understand.
  
  

2 Method 2: Obtain by adding up the number of days in each month

  
The second problem-solving idea of ​​the above case is:
  
Step1: Call the calendar library to apply the for loop to obtain the number of days in each month in which the input date is less than the current month in the year, and add them up.
  
Step2: Obtain the number of days in the current month of the date, and sum it with the number of days before, to find the day of the year that this day is.
  
The specific code is as follows:

import calendar

raw_date = input('请按yyyymmdd方式输入年月日')
def y_days2(raw_date):
    all_days = 0
    for i in range(1, int(raw_date[4:6])):
        all_days += calendar.monthrange(int(raw_date[0:4]), i)[1]
    all_days += int(raw_date[6:8])
    print(all_days)
    return all_days
y_days2(raw_date)

If the manual date is 20220108, the result is:

8

You can manually verify that the result is correct, and interested friends can also copy the code into Python and enter another date for verification.
  
So far, the classic case of programming in Python [examination question] has been explained to determine the date of the year. If you want to know more about functions in Python, you can read related articles on the "Learning Python" module in the official account.
  
You may be interested in:
Draw Pikachu with Python
Draw a word cloud map
with Python Draw 520 eternal heart beats with Python Python face recognition - you are the only one
in my eyes With sound and text) Use the py2neo library in Python to operate neo4j and build a relationship map Python romantic confession source code collection (love, rose, photo wall, confession under the stars)



Guess you like

Origin blog.csdn.net/qq_32532663/article/details/126568038