当前位置 : 主页 > 编程语言 > python >

python中 + += append extend区别

来源:互联网 收集:自由互联 发布时间:2022-06-15
a = [1,2] c = a + [3,4] print(c) 输入出:[1, 2, 3, 4] a = [1,2] c = a + [3,4] #就地加 a += [3,4] print(a) 输出结果: [1, 2, 3, 4] a = [1,2] # c = a + (3,4) #就地加 a += [3,4] print(a) 运行提示错误: TypeError: can only
a = [1,2]
c = a + [3,4]
print(c)


输入出:[1, 2, 3, 4]


a = [1,2]
c = a + [3,4]

#就地加
a += [3,4]
print(a)

输出结果:
[1, 2, 3, 4]


a = [1,2]
# c = a + (3,4)

#就地加
a += [3,4]
print(a)

运行提示错误:
TypeError: can only concatenate list (not "tuple") to list


1 a = [1,2]
2 a.append((1,2))
3 print(a)
4 运行结果:
5 [1, 2, (1, 2)]


a = [1,2]
a.extend([5,6,7])
print(a)
运行结果:
[1, 2, 5, 6, 7]


上一篇:python对象的自省机制
下一篇:没有了
网友评论