1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import datetime

def is_leap_year(year):
#判断是否闰年
is_leap = False;
if year %4==0 and year %100 !=0 or year %400 ==0:
return True

def main():
input_data_str = input("请输入日期(yyyy/mm/dd):")
input_data = datetime.datetime.strptime(input_data_str, "%Y/%m/%d")
#print(input_data)

year = input_data.year
month = input_data.month
day = input_data.day

#使用元组
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 month>=2 and is_leap_year(year):
days+=1

#使用列表
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
#计算月份天数总和
days = sum(days_in_month[:month-1]) + day

print("{0}, 这是第 {1} 天".format(input_data_str,days))

if __name__ == '__main__':
main()

元组

示例:('red','blue','green')(2,5,67,8,9)

一旦被创建就不能被改变,元组元素可以是不同类型的,列表通常由相同类型数据组成
元素存在先后关系,可以通过索引访问元组中的元素
元组表示结构,列表表示顺序

集合

0或多个无序的组合,元素不可重复
不可以通过索引获取元素
set()函数用于集合的生成,集合表示成员间的关系、元素去重
1
2
3
4
5
6
7
8
9
10
11
12
13
14
s = {1,4,6,8}
t = {1,3,7,8}

s - 7
# -> 4,6

s & t
# -> 1,8

s | t
# -> 1,3,4,6,7,8

s ^ t
# -> 3,4,6,7
1
2
3
4
5
6
7
8
9
def get_days_in_month(year,month):
if month in {1,3,5,7,8,10,12}:
return 31
elif month in {4,6,9,11}:
return 30
elif is_leap_year(year):
return 29
else:
return 28

字典

“键-值” 组合,通过映射查找数据项
映射:通过任意键查找集合中的值得过程
字典类型以键为索引,一个键对应一个值,字典类型的数据是无序的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
dic = {"Eggs":2.59, "Milk":3.16}

# 增加
dic["Cheese"] = 4.80
# -> {"Eggs":2.59, "Milk":3.16, "Cheese":4.80}

# 访问
print(dic["Eggs"])
# -> 2.59

# 修改
dic["Eggs"] = 8.80
# -> {"Eggs":8.80, "Milk":3.16, "Cheese":4.80}

# 删除
del dic["Eggs"]
# -> {"Milk":3.16, "Cheese":4.80}

# 是否存在
“Eggs” in dic
# -> False

# 获取所有的 key
dic.keys()

# 获取所有的 values
dic.values()

# 遍历所有的数据项
dic.items()
1
2
3
4
5
6
7
8
9
10
11
12
13
14

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
}
days = 0
def get_total_days(year,month):
global month_day_dict
global days
for i in range(1, month):
days += month_day_dict[i]

if month>2 and is_leap_year(year):
days +=1