元组的元素不可被修改,但元组的元素的元素可以被修改
#!/usr/bin/python3 name = "alex" print("I am %s"%name) a = (1,2,3,4,5,6,{"k1":"v1"}) #元组 print(a[1:5]) print(a) a[6]["k1"] = 100 print(a)
以上代码执行后的结果:
I am alex (2, 3, 4, 5) (1, 2, 3, 4, 5, 6, {'k1': 'v1'}) (1, 2, 3, 4, 5, 6, {'k1': 100})
列表的元素不可被修改,但列表的元素的元素可以被修改
#!/usr/bin/python3 name = "alex" print("I am %s"%name) a = [1,2,3,4,5,6,{"k1":"v1"}] # 列表 print(a[1:5]) print(a) a[6]["k1"] = 100 print(a)
以上代码执行后的结果:
I am alex [2, 3, 4, 5] [1, 2, 3, 4, 5, 6, {'k1': 'v1'}] [1, 2, 3, 4, 5, 6, {'k1': 100}]