大家好,又见面了,我是你们的朋友全栈君。
python append描述 append函数可以在列表的末尾添加新的对象。函数无返回值,但是会修改列表。
list.append(object) 名称 说明 备注 list 待添加元素的列表 object 将要给列表中添加的对象 不可省略的参数 3 examples to append list in python
test = [‘Python’, ‘C’, ‘Java’]
test.append(5) test.append(23.6) test.append(‘HTML’)
print(test) 输出结果为:
[‘Python’, ‘C’, ‘Java’, 5, 23.6, ‘HTML’]
test = [‘Python’, ‘C’, ‘Java’]
test.append([‘Windows’, 2018, ‘OpenStack’]) test.append((‘Huawei’, ‘Tencent’)) test.append({‘Nova’:‘virtual compute service’, ‘Neutron’:‘net service’})
print(test) 输出结果为:
[‘Python’, ‘C’, ‘Java’, [‘Windows’, 2018, ‘OpenStack’], (‘Huawei’, ‘Tencent’), {‘Nova’: ‘virtual compute service’, ‘Neutron’: ‘net service’}]
test = [‘Python’, ‘C’, ‘Java’]
test.append(None)
print(test) 输出结果为:
[‘Python’, ‘C’, ‘Java’, None] 注意事项 object参数不能省略,否则Python会报错:
test = [‘Python’, ‘C’, ‘Java’]
test.append()
print(test) Traceback (most recent call last): File “/Users/untitled3/Test2.py”, line 3, in test.append() TypeError: append() takes exactly one argument (0 given) 如果想给列表末尾添加空元素,应该将参数写为None
3 examples to append list in python
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/149075.html原文链接:https://javaforall.cn
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有