1 classmates = ['Michael', 'Bob', 'Tracy'] 2 print(classmates) 3 print(len(classmates)) 4 print(classmates[-1]) 5 classmates.append("adma") 6 print(classmates) 7 print(classmates.__len__()) 8 classmates.insert(0,"tony") 9 print(classmates) 10 classmates.pop() 11 print(classmates) 12 classmates.pop(-2) 13 print(classmates) 14 """ 15 元组定义,不能改元素的list,定义用(),list用[] 16 """ 17 t = (1, 2) 18 print(t) 19 t = ('a', 'b', ['A', 'B']) 20 print(t[2][0])#A 21 t[2][0] = 'X' 22 t[2][1] = 'Y' 23 print(t)#('a', 'b', ['X', 'Y']) 24 """ 25 表面上看,tuple的元素确实变了,但其实变的不是tuple的元素,而是list的元素。 26 tuple一开始指向的list并没有改成别的list, 27 所以,tuple所谓的“不变”是说,tuple的每个元素,指向永远不变。 28 """ 29 tup1 = (12, 34.56) 30 tup2 = ('abc', 'xyz') 31 tup3 = tup1 + tup2 32 print(tup3) 33 print(id(tup3))#1830292833440 34 tup3 = (1,2) 35 print(tup3) 36 print(id(tup3))#1830285448000 # 内存地址不一样了 37 #从以上实例可以看出, 38 # 重新赋值的元组 tup, 39 # 绑定到新的对象了,不是修改了原来的对象。
标签:__,classmates,tuple,python,list,tup3,print From: https://www.cnblogs.com/lingxingzi/p/16807857.html