ITPub博客

首页 > IT职业 > IT职场 > 在python中append()函数能做什么

在python中append()函数能做什么

IT职场 作者:xduu 时间:2021-09-11 10:17:20 0 删除 编辑

python中的append()方法用于在列表末尾添加新的对象。

append()方法语法:

list.append(obj)

参数

obj -- 添加到列表末尾的对象。

返回值:该方法无返回值,但是会修改原来的列表。

示例:

ls1 = [1,2,3,4,5,6]
ls2 = [1,2,3,4,5,6]
ls1.append(12)
 
#可以添加列表,字典,元组,集合,字符串等
ls2.append([1,"a"])  #添加列表
ls2.append({2:"a",3:"hj"}) #添加字典
ls2.append((1,"k",3)) #添加元组
ls2.append({"1","2","h"}) #添加集合
ls2.append("123abc") #添加字符串
 
print(ls1.append(12)) #无返回值
print(ls1) #append()函数的操作对象是原列表。
print(ls2)

运行结果:

None
[1, 2, 3, 4, 5, 6, 12, 12]
[1, 2, 3, 4, 5, 6, [1, 'a'], {2: 'a', 3: 'hj'}, (1, 'k', 3), {'2', '1', 'h'}, '123abc']

更多Python知识请关注Python视频教程栏目。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2195/viewspace-2833743/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-16

  • 博文量
    84
  • 访问量
    1000