标签:地址 变化 创建 int 两种 strong name 不可变类 evel
字典
1.查询内存地址
a = 10
print(id(a))
b = a
print(id(b))
b = 15
print(id(b))
2. 数据类型
不可变类型:整型、字符串、元组
可变类型:列表、字典
字典的值可以是任意类型,键只能是不可变类型。
字典存储是无序的。例如:
dic = {‘name‘:‘ysw‘,‘age‘:36,‘hobby‘:‘game‘,‘is_handsome‘:True}
print(dic)
print(dic[‘name‘])
#dic = {1:‘ysw‘,‘age‘:36,‘hobby‘:‘game‘,‘is_handsome‘:True}
#print(dic)
#print(dic[1])
#dic = {[1,2]:‘ysw‘,‘age‘:36,‘hobby‘:‘game‘,‘is_handsome‘:True}
#print(dic)
#print(dic[[1,2]])
#dic = {‘name‘:‘ysw‘,‘age‘:36,‘hobby‘:{‘game_name‘:‘wow‘,‘level‘:‘20‘},‘is_handsome‘:True}
#print(dic[‘hobby‘])
#dic = {{‘1‘:‘123‘},‘age‘:36,‘hobby‘:{‘game_name‘:‘wow‘,‘level‘:‘20‘},‘is_handsome‘:True}
#print(dic)
#字典两大特点:无序;键唯一
#dic = {‘age‘:‘ysw‘,‘age‘:36,‘hobby‘:‘game‘,‘is_handsome‘:True}
#print(dic)
3.字典的两种创建方式:
#dic1 = {‘name‘:‘ysw‘}
#dic2 = dict(((‘name‘,‘ysw‘),))
4.对应操作
(1)增
dic1 = {‘name‘:‘ysw‘}
dic1[‘age‘] = 18
print(dic1)
dic1[‘name‘] = 18
print(dic1)
#没有重复的键就添加,有重复的键就替换数值
------------------------------
dic1 = {‘name‘:‘ysw‘}
dic1[‘age‘] = 18
print(dic1)
dic1.setdefault(‘age‘,36)
print(dic1)
#dic1.setdefault(‘hobby‘,‘game‘)
#print(dic1)
#setdefault的特点:没有重复的键就添加,有重复的键就没有变化
标签:地址 变化 创建 int 两种 strong name 不可变类 evel
原文地址:https://www.cnblogs.com/pl-2018/p/9500850.html