list 有序集合,隨時增刪 [] tuple 有序列表,一旦初始化,無法修改 () dict 字典無序 鍵值對(key-value)方式存儲,查找速度快 {} set 集合無序,且不可重複 """ auther: version:v1.0 date: fuction:Enter the year, month, and day to determine which day of the year Use tuple """ from datetime import datetime def main(): input_date_str = raw_input('Please enter the date(yyyy/mm/dd):') # print input_date_str input_date = datetime.strptime(input_date_str,'%Y/%m/%d') print input_date year = input_date.year month = input_date.month day = input_date.day #Calculate the sum of the days of the previous month and the current month days_in_month_tup = (31,28,31,30,31,30,31,31,30,31,30,31) days = sum(days_in_month_tup[:month-1])+day if (year % 400 == 0) or ((year % 4==0) and (year % 100 !=0)): if month > 2: days += 1 print 'This is the {} day.'.format(days) if __name__ == '__main__': main()
""" auther: version:v2.0 date: fuction:Enter the year, month, and day to determine which day of the year Use list """ from datetime import datetime def is_leap_year(year): is_leap = False if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)): is_leap = True return is_leap def main(): input_date_str = raw_input('Please enter the date(yyyy/mm/dd):') # print input_date_str input_date = datetime.strptime(input_date_str,'%Y/%m/%d') print input_date year = input_date.year month = input_date.month day = input_date.day #Calculate the sum of the days of the previous month and the current month days_in_month_list = [31,28,31,30,31,30,31,31,30,31,30,31] if is_leap_year(year): days_in_month_list[1] = 29 print days_in_month_list[1] days = sum(days_in_month_list[:month-1])+day # days = sum(days_in_month_list[:month-1])+day # # if month > 2 and is_leap_year(year): # days += 1 print 'Year :{} ,Day :{} .'.format(year,days) if __name__ == '__main__': main()
""" auther: version:v3.0 date: fuction:Enter the year, month, and day to determine which day of the year Use set """ from datetime import datetime def is_leap_year(year): is_leap = False if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)): is_leap = True return is_leap def main(): input_date_str = raw_input('Please enter the date(yyyy/mm/dd):') # print input_date_str input_date = datetime.strptime(input_date_str,'%Y/%m/%d') print input_date year = input_date.year month = input_date.month day = input_date.day _30_days_month_set = {4,6,9,11} _31_days_month_set = {1,3,5,7,8,10,12} days = 0 days += day for i in range(1,month): if i in _30_days_month_set: days += 30 elif i in _31_days_month_set: days += 31 else: days += 28 if is_leap_year(year) and month > 2: days +=1 print 'Year :{} ,Day :{} .'.format(year,days) if __name__ == '__main__': main()
""" auther: version:v4.0 date: fuction:Enter the year, month, and day to determine which day of the year Use dict """ from datetime import datetime def is_leap_year(year): is_leap = False if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)): is_leap = True return is_leap def main(): input_date_str = raw_input('Please enter the date(yyyy/mm/dd):') # print input_date_str input_date = datetime.strptime(input_date_str,'%Y/%m/%d') print input_date year = input_date.year month = input_date.month day = input_date.day month_day_dict = {1:31, 2:28, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31} # day_month_dict = {30:{4,6,9,11}, # 31:{1,3,5,7,8,10,12} # } days = 0 days += day for i in range(1,month): days += month_day_dict[i] if is_leap_year(year) and month > 2: days +=1 print 'Year :{} ,Day :{} .'.format(year,days) if __name__ == '__main__': main()