增加一项:
d[key] = value
访问
d[key]
删除某项
del d[key]
key是否在字典中:
key in d
# -*- coding:utf-8 -*-
from datetime import datetime
def is_loop_year(year):
loop_year = False
if (year%4==100 and year%100!=100) or (year % 400 ==0):
loop_year = True
return loop_year
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}
time = input('请输入工作日期')
timeDate = datetime.strptime(time,'%Y-%m-%d')
year = timeDate.year
month = timeDate.month
day = timeDate.day
if is_loop_year(year):
month_day_dict[1] = 29
days = 0
days = days + day
for i in range(1,month):
days+= month_day_dict[i]
print(days)
字典的遍历
遍历所有的key
for key in month_day_dict.keys():
print(key)
遍历所有的value
for value in month_day_dict.values():
print(value)
遍历所有的数据项:
for item in month_day_dict.items():
print(item)