判断第几天
V1.0
案例描述:
- 输入某年某月某日,判断这一天是这一年的第几天?
- Demo:
- 输入的日期为2017/03/05,是2017年的第几天?
- 输入的日期为2012/03/05,是2012年的第几天?
案例分析:
- 每个月份的天数不同
- 闰年与平年的2月份天数不同
- 闰年判断:
- 四年一闰且百年不闰
- 或四百年再闰
上机实验:

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()
补充说明:
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()
补充说明:
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()
补充说明:
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()
补充说明:
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)