my_tuple = (1,2,3,4,5)
print(my_tuple,type(my_tuple)) # (1, 2, 3, 4, 5)
my_tuple[3] = 8 # TypeError: 'tuple' object does not support item assignment
a_tuple = (1)
print(a_tuple,type(a_tuple)) # 1
a_tuple = (1,)
print(a_tuple,type(a_tuple)) # (1,)
my_tuple = 1,2,3,4,5
print(my_tuple, type(my_tuple)) # (1, 2, 3, 4, 5)
a,b,*c = my_tuple
print('a =',a) # a = 1
print('b =',b) # b = 2
print('c =',c) # c = [3, 4, 5]
a,*b,c = my_tuple
print('a =',a) # a = 1
print('b =',b) # b = [2, 3, 4]
print('c =',c) # c = 5
*a,b,c = my_tuple
print('a =',a) # a = [1, 2, 3]
print('b =',b) # b = 4
print('c =',c) # c = 5
*a,*b,c = my_tuple # SyntaxError: two starred expressions in assignment
print('a =',a)
print('b =',b)
print('c =',c)
a,b,*c = 'hello python'
print('a =',a) # a = h
print('b =',b) # b = e
print('c =',c) # c = ['l', 'l', 'o', ' ', 'p', 'y', 't', 'h', 'o', 'n']
a,b,*c = [1,2,3,4,5,6]
print('a =',a) # a = 1
print('b =',b) # b = 2
print('c =',c) # c = [3, 4, 5, 6]
d = {'name':'钢铁侠','age':38,'sex':'男'}
# 需要根据键来获取值
print(d['name'],d['age'],d['sex']) # 钢铁侠 38 男
d = dict(name='钢铁侠',age=35,sex='男')
print(d,type(d)) # {'name': '钢铁侠', 'age': 35, 'sex': '男'}
d = dict([('name','钢铁侠'),('age',35)])
print(d,type(d)) # {'name': '钢铁侠', 'age': 35}
d = {'name':'钢铁侠','age':38,'sex':'男'}
print(len(d)) # 3
d = {'name':'钢铁侠','age':38,'sex':'男'}
print('name' not in d) # False
d = {'name':'钢铁侠','age':38,'sex':'男'}
print(d['age']) # 38
d = {'name':'钢铁侠','age':38,'sex':'男'}
print(d.get('age')) # 38
print(d.get('job')) # None
print(d.get('birthday','这个key值不存在')) # 这个key值不存在
d = {'name':'钢铁侠','age':38,'sex':'男'}
d['name'] = '葫芦娃'
print(d) # {'name': '葫芦娃', 'age': 38, 'sex': '男'}
d = {'name':'钢铁侠','age':38,'sex':'男'}
d['phone'] = '12345678'
print(d) # {'name': '钢铁侠', 'age': 38, 'sex': '男', 'phone': '12345678'}
d = {'name':'钢铁侠','age':38,'sex':'男'}
result = d.setdefault('name','葫芦娃')
print(result) # 钢铁侠
print(d) # {'name': '钢铁侠', 'age': 38, 'sex': '男'}
d = {'name':'钢铁侠','age':38,'sex':'男'}
result = d.setdefault('hello','葫芦娃')
print(result) # 葫芦娃
print(d) # {'name': '钢铁侠', 'age': 38, 'sex': '男', 'hello': '葫芦娃'}
d1 = {'a':1,'b':2,'c':3}
d2 = {'d':4,'e':5,'f':6}
d1.update(d2)
print(d1) # {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}
print(d2) # {'d': 4, 'e': 5, 'f': 6}
d1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}
del d1['a']
del d1['b']
print(d1) # {'c': 3, 'd': 4, 'e': 5, 'f': 6}
d1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}
result = d1.popitem()
print(result) # ('f', 6)
print(d1) # {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
d1 = {'c': 3, 'd': 4, 'e': 5}
result = d1.pop('b','这个key值不存在')
print(result) # 这个key值不存在
print(d1) # {'c': 3, 'd': 4, 'e': 5}
result = d1.pop('c')
print(result) # 3
print(d1) # {'d': 4, 'e': 5}
d1 = {'c': 3, 'd': 4, 'e': 5}
d1.clear()
print(d1) # {}
d = {'a':{'name':'黑猫警长','age':18},'b':2,'c':3}
d1 = d
print('d =',d,id(d)) # d = {'a': {'name': '黑猫警长', 'age': 18}, 'b': 2, 'c': 3} 3039239036648
print('d1 =',d1,id(d1)) # d1 = {'a': {'name': '黑猫警长', 'age': 18}, 'b': 2, 'c': 3} 3039239036648
d1['b'] = 20
print('d =',d,id(d)) # d = {'a': {'name': '黑猫警长', 'age': 18}, 'b': 20, 'c': 3} 3039239036648
print('d1 =',d1,id(d1)) # d1 = {'a': {'name': '黑猫警长', 'age': 18}, 'b': 20, 'c': 3} 3039239036648
d1['a']['name'] = '皮卡丘'
print('d =',d,id(d)) # d = {'a': {'name': '皮卡丘', 'age': 18}, 'b': 20, 'c': 3} 3039239036648
print('d1 =',d1,id(d1)) # d1 = {'a': {'name': '皮卡丘', 'age': 18}, 'b': 20, 'c': 3} 3039239036648
d = {'a':{'name':'黑猫警长','age':18},'b':2,'c':3}
d2 = d.copy()
print('d =',d,id(d)) # d = {'a': {'name': '黑猫警长', 'age': 18}, 'b': 2, 'c': 3} 2039041318632
print('d2 =',d2,id(d2)) # d2 = {'a': {'name': '黑猫警长', 'age': 18}, 'b': 2, 'c': 3} 2039041347728
d2['b'] = 20
print('d =',d,id(d)) # d = {'a': {'name': '黑猫警长', 'age': 18}, 'b': 2, 'c': 3} 2039041318632
print('d2 =',d2,id(d2)) # d2 = {'a': {'name': '黑猫警长', 'age': 18}, 'b': 20, 'c': 3} 2039041347728
d2['a']['name'] = '皮卡丘'
print('d =',d,id(d)) # d = {'a': {'name': '皮卡丘', 'age': 18}, 'b': 2, 'c': 3} 2039041318632
print('d2 =',d2,id(d2)) # d2 = {'a': {'name': '皮卡丘', 'age': 18}, 'b': 20, 'c': 3} 2039041347728
# 修改d2中‘a’对应的字典,d中的字典也变了,说明字典{'name': '皮卡丘', 'age': 18}没有被复制。
d = {'name':'钢铁侠','age':38,'sex':'男'}
print(d.keys()) # dict_keys(['name', 'age', 'sex'])
list_keys = []
for k in d.keys():
list_keys.append(k)
print(list_keys) # ['name', 'age', 'sex']
d = {'name':'钢铁侠','age':38,'sex':'男'}
print(d.values()) # dict_values(['钢铁侠', 38, '男'])
list_values = []
for k in d.values():
list_values.append(k)
print(list_values) # ['钢铁侠', 38, '男']
d = {'name':'钢铁侠','age':38,'sex':'男'}
print(d.items())
for k,v in d.items():
print(k,'=',v)
# 运行结果:
# dict_items([('name', '钢铁侠'), ('age', 38), ('sex', '男')])
# name = 钢铁侠
# age = 38
# sex = 男
集合 set 和列表非常相似,但也有不同点:
# 集合中只能存储不可变对象
s = {[1,2,3],[4,5,6]} # TypeError: unhashable type: 'list'
print(s,type(s))
# 集合中存储的对象是无序的
s = {'a','b',1,2,3}
print(s[0]) # TypeError: 'set' object does not support indexing
# 集合中不能出现重复的元素
s = {2,3,1,4,2,2,3,1}
print(s,type(s)) # {1, 2, 3, 4}
s = {}
print(s,type(s)) # {}
s = {1,2,3}
print(s,type(s)) # {1, 2, 3}
# set() 可以将序列和字典转换为集合
s = set()
print(s,type(s)) # set()
s = set([4,2,1,5,3,1,2,3,5,5])
print(s,type(s)) # {1, 2, 3, 4, 5}
s = set('hello')
print(s,type(s)) # {'h', 'o', 'e', 'l'}
# 使用set()函数将字典转换为集合的时候,只会包含字典中的键
s = set({'a':1,'b':2,'c':3})
print(s,type(s)) # {'a', 'c', 'b'}
s = {'a','b',1,2,3}
print(s,type(s)) # {1, 2, 3, 'b', 'a'}
a = list(s)
print(a,type(a)) # [1, 2, 3, 'b', 'a']
s = {'a','b',1,2,3}
print('a' in s) # True
print('a' not in s) # False
s = {'a','b',1,2,3}
print(len(s)) # 5
s = {'a','b',1,2,3}
s.add(4)
print(s) # {1, 2, 3, 4, 'a', 'b'}
s = {'a','b',1,2,3}
s2 = set('hello')
s.update(s2)
print(s) # {'b', 2, 3, 1, 'h', 'o', 'e', 'l', 'a'}
print(s2) # {'l', 'h', 'o', 'e'}
s = {'a','b',1,2,3}
r = s.pop()
print(s) # {1, 2, 3, 'a'}
print(r) # b
s = {'a','b',1,2,3}
s.remove('a')
print(s) # {1, 2, 3, 'b'}
s = {'a','b',1,2,3}
s.clear()
print(s) # set()
s1 = {1,2,3,4,5}
s2 = {3,4,5,6,7}
r = s1 & s2
print(r) # {3, 4, 5}
s1 = {1,2,3,4,5}
s2 = {3,4,5,6,7}
r = s1 | s2
print(r) # {1, 2, 3, 4, 5, 6, 7}
s1 = {1,2,3,4,5}
s2 = {3,4,5,6,7}
r1 = s1 - s2
print(r1) # {1, 2}
r2 = s2 - s1
print(r2) # {6, 7}
s1 = {1,2,3,4,5}
s2 = {3,4,5,6,7}
r = s1 ^ s2
print(r) # {1, 2, 6, 7}
a = {1,2,3}
b = {1,2,3,4,5}
c = {1,2,3}
r1 = a <= b
print(r1) # True
r2 = a < b
print(r2) # True
r3 = a < c
print(r3) # False
a = [1,2,3]
print('修改前',a,id(a)) # 修改前 [1, 2, 3] 2537002821000
a[0] = 10
print('修改后',a,id(a)) # 修改后 [10, 2, 3] 2537002821000
a = [1,2,3]
print('修改前',a,id(a)) # 修改前 [1, 2, 3] 1669417788296
a = [4,5,6]
print('修改后',a,id(a)) # 修改后 [4, 5, 6] 1669417788808
a = [1,2,3]
b = a
b[0] = 10
print('a',a,id(a)) # a [10, 2, 3] 2114062766984
print('b',b,id(b)) # b [10, 2, 3] 2114062766984
a = [1,2,3]
b = a
b = [7,8,9]
print('a',a,id(a)) # a [1, 2, 3] 1662926902152
print('b',b,id(b)) # b [7, 8, 9] 1662926901640