【每天學點Python】案例五:判斷第幾天


判斷第幾天

V1.0

案例描述:

  • 輸入某年某月某日,判斷這一天是這一年的第幾天?
  • Demo:
    • 輸入的日期為2017/03/05,是2017年的第幾天?
    • 輸入的日期為2012/03/05,是2012年的第幾天?

案例分析:

  1. 每個月份的天數不同
  2. 閏年與平年的2月份天數不同
  3. 閏年判斷:
    • 四年一閏百年不閏
    • 四百年再閏

上機實驗:

 1 """
 2     作者:王鑫正
 3     版本:1.0
 4     日期:2018年9月24日
 5     功能:輸入某年某月某日,判斷這一天是這一年的第幾天?
 6 """
 7 
 8 from datetime import datetime
 9 
10 
11 def main():
12     """
13         主函數
14     """
15     input_date_str = input('請輸入日期(yyyy-mm-dd):')
16     input_date = datetime.strptime(input_date_str, '%Y-%m-%d')
17     print(input_date)
18 
19     year = input_date.year
20     month = input_date.month
21     day = input_date.day
22 
23     # 計算之前月份天數的總和以及當前月份天數
24     days_in_month_tup = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
25     # print(days_in_month_tup[: month - 1])
26     days = sum(days_in_month_tup[: month - 1]) + day
27 
28     if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
29         if month > 2:
30             days += 1
31 
32     print('這是第{}天:'.format(days))
33 
34 
35 if __name__ == '__main__':
36     main()
View Code

補充說明:

1. 元組

  • 元組(tuple)是特殊的序列類型
  • 一旦被創建就不能修改,使得代碼更安全
  • 使用逗號和圓括號來表示,如('red', 'blue', 'green'), (2, 4, 6)
  • 訪問方式和列表相同
  • 一般用於表達固定數據項、函數多返回值等情況

2. 特點

  • 元組中的元素可以是不同類型的
  • 元組中個元素存在先后關系,可通過索引訪問元組中元素

 


V2.0增加功能:用列表替換元組

上機實驗:

 1 """
 2     作者:王鑫正
 3     版本:2.0
 4     日期:2018年9月24日
 5     功能:輸入某年某月某日,判斷這一天是這一年的第幾天?
 6     2.0增加功能:用列表替換元組
 7 """
 8 
 9 from datetime import datetime
10 
11 
12 def is_leap_year(year):
13     """
14         判斷year是否為閏年
15         是:返回True
16         否:返回False
17     """
18     is_leap = False
19 
20     # 判斷閏年
21     if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
22         is_leap = True
23 
24     return is_leap
25 
26 
27 def main():
28     """
29         主函數
30     """
31     input_date_str = input('請輸入日期(yyyy-mm-dd):')
32     input_date = datetime.strptime(input_date_str, '%Y-%m-%d')
33     print(input_date)
34 
35     year = input_date.year
36     month = input_date.month
37     day = input_date.day
38 
39     # 計算之前月份天數的總和以及當前月份天數
40     days_in_month_list = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
41 
42     # 判斷閏年
43     if is_leap_year(year):
44         days_in_month_list[1] = 29
45 
46     days = sum(days_in_month_list[: month - 1]) + day
47 
48     print('這是{}年的第{}天'.format(year, days))
49 
50 
51 if __name__ == '__main__':
52     main()
View Code

補充說明:

1. 列表與元組

  • 元組是不可變的,列表是可變的
  • 元組通常由不同的數據組成,列表通常是由相同類型的數據組成
  • 元組表示的是結構,列表表示的是順序

 


V3.0增加功能: 將月份划分為不同的集合再操作

上機實驗:

 1 """
 2     作者:王鑫正
 3     版本:3.0
 4     日期:2018年9月24日
 5     功能:輸入某年某月某日,判斷這一天是這一年的第幾天?
 6     3.0增加功能: 將月份划分為不同的集合再操作
 7 """
 8 
 9 from datetime import datetime
10 
11 
12 def is_leap_year(year):
13     """
14         判斷year是否為閏年
15         是:返回True
16         否:返回False
17     """
18     is_leap = False
19 
20     # 判斷閏年
21     if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
22         is_leap = True
23 
24     return is_leap
25 
26 
27 def main():
28     """
29         主函數
30     """
31     input_date_str = input('請輸入日期(yyyy-mm-dd):')
32     input_date = datetime.strptime(input_date_str, '%Y-%m-%d')
33     print(input_date)
34 
35     year = input_date.year
36     month = input_date.month
37     day = input_date.day
38 
39     # 包含30天的月份集合
40     _30_days_month_set = {4, 6, 9, 11}
41     _31_days_month_set = {1, 3, 5, 7, 8, 10, 12}
42 
43     # 初始化值
44     days = day
45 
46     for i in range(1, month):
47         if i in _30_days_month_set:
48             days += 30
49         elif i in _31_days_month_set:
50             days += 31
51         else:
52             days += 28
53 
54     if is_leap_year(year) and month > 2:
55         days += 1
56 
57     print('這是{}年的第{}天'.format(year, days))
58 
59 
60 if __name__ == '__main__':
61     main()
View Code

補充說明:

1. 集合

  • Python中的集合(Set)類型同數學中的集合概念一致,即包含0或多個數據項的無序組合
  • 集合中的元素不可重復
  • 集合是無序組合,沒有索引和位置的概念
  • set()函數用於集合的生成,返回結果是一個無重復且排序任意的集合
  • 集合通常用於表示成員間的關系、元素去重等

2. 集合操作

 

 


 V4.0增加功能:將月份及其對應天數通過字典表示

上機實驗:

 1 """
 2     作者:王鑫正
 3     版本:4.0
 4     日期:2018年9月24日
 5     功能:輸入某年某月某日,判斷這一天是這一年的第幾天?
 6     4.0增加功能:將月份及其對應天數通過字典表示
 7 """
 8 
 9 from datetime import datetime
10 
11 
12 def is_leap_year(year):
13     """
14         判斷year是否為閏年
15         是:返回True
16         否:返回False
17     """
18     is_leap = False
19 
20     # 判斷閏年
21     if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
22         is_leap = True
23 
24     return is_leap
25 
26 
27 def main():
28     """
29         主函數
30     """
31     input_date_str = input('請輸入日期(yyyy-mm-dd):')
32     input_date = datetime.strptime(input_date_str, '%Y-%m-%d')
33     print(input_date)
34 
35     year = input_date.year
36     month = input_date.month
37     day = input_date.day
38 
39     # 月份-天數 字典
40     month_day_dict = {1: 31,
41                       2: 28,
42                       3: 31,
43                       4: 30,
44                       5: 31,
45                       6: 30,
46                       7: 31,
47                       8: 31,
48                       9: 30,
49                       10: 31,
50                       11: 30,
51                       12: 31}
52 
53     # 初始化值
54     days = day
55 
56     for i in range(1, month):
57         days += month_day_dict[i]
58 
59     if is_leap_year(year) and month > 2:
60         days += 1
61 
62     print('這是{}年的第{}天'.format(year, days))
63 
64 
65 if __name__ == '__main__':
66     main()
View Code

補充說明:

1. 字典

  • 字典類型(dict)是“鍵-值”數據項的組合,每個元素是一個鍵值對,如:身份證號(鍵)- 個人信息(值)
  • 字典類型數據通過映射查找數據項
  • 映射:通過任意鍵查找集合中的值的過程
  • 字典類型以鍵為索引,一個鍵對應一個值
  • 字典類型的數據是無序的

2. 字典操作

  • 增加一項:d[key] = value
  • 訪問:d[key]
  • 刪除某項:del d[key]
  • key是否在字典中:key in d

3. 字典的遍歷

  • 遍歷所有的key:
1 for key in d.keys():
2     print(key)
  • 遍歷所有的value:
1 for value in d.values():
2     print(value)
  • 遍歷所有的數據項:
1 for item in d.items():
2     print(item)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM