标签:报错 default font items bsp div 元组 print 循环
# 字典
# 增
dic1 = {‘name‘:‘yasuo‘,‘age‘:‘20‘,‘weight‘:‘56‘,‘high‘:‘178‘}
dic1[‘age‘] = 18
print(dic1)
dic1[‘like‘] = ‘wind‘ # 没有就添加,有就覆盖
print(dic1)
dic1.setdefault(‘weight‘,60) # 有就不改变,没有就添加
# 删
dic1.pop(‘high‘) # 有返回值,按键去删除
print(dic1.pop(‘high1‘,‘没有这个键‘)) # 返回值可以设置
print(dic1)
print(dic1.popitem()) # 随机删除,有返回值,元组里面是删除的健值
print(dic1)
del dic1[‘high‘]
print(dic1)
dic1.clear() # 清空字典
# 改
dic1[‘age‘] = 18
print(dic1)
dic2 = {‘name‘:‘yasuo‘,‘weight‘:‘60‘}
dic2.update(dic1)
print(dic1) #把dic1更新到dic2中,没有的就添加,有的不一样就覆盖
# 查
print(dic1.keys())
print(dic1.values())
print(dic1.items())
for i in dic1:
print(i) #循环打印键
for k,v in dic1.items():#items要记得
print(k,v)
v1 = dic1[‘name‘]
print(v1)
v2 =dict[‘name1‘] # 没有就报错
print(v2)
print(dic1.get(‘name‘))
print(dic1.get(‘name1‘))
print(dic1.get(‘name1‘,‘没有这个键‘)) # 不会报错
标签:报错 default font items bsp div 元组 print 循环
原文地址:https://www.cnblogs.com/8856S/p/9690441.html