Python中的append()方法

2022-10-22,,

Python中的append()方法

  • append()

append()

append()方法表示在原来的列表末尾追加新的对象。

如我们在一个数组的后面添加一个元素

x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
x1 = (2)
x2 = []
def fun(i):
    i.append(x1)
    return i
y1 = fun(x)
y2 = fun(x2)
print('在数组x:后加入元素2:', y1)
print('在数组x2中添加元素2:', y2)

-----------------------------------------------
输出结果:

在数组x后加入元素2: [1, 2, 3, 4, 5, 6, 7, 8, 9, 2]
在数组x2中添加元素2: [2]

《Python中的append()方法.doc》

下载本文的Word格式文档,以方便收藏与打印。