Skip to content

Instantly share code, notes, and snippets.

@tcpdump-examples
Created January 18, 2022 13:35
Show Gist options
  • Save tcpdump-examples/a06dfb2c291091e091f39e3f7f40e1ce to your computer and use it in GitHub Desktop.
Save tcpdump-examples/a06dfb2c291091e091f39e3f7f40e1ce to your computer and use it in GitHub Desktop.

python append描述 append函数可以在列表的末尾添加新的对象。函数无返回值,但是会修改列表。

append语法

list.append(object) 名称 说明 备注 list 待添加元素的列表 object 将要给列表中添加的对象 不可省略的参数 3 examples to append list in python

append举例

1. 给列表中添加整数、浮点数和字符串:

test = ['Python', 'C', 'Java']

test.append(5) test.append(23.6) test.append('HTML')

print(test) 输出结果为:

['Python', 'C', 'Java', 5, 23.6, 'HTML']

2. 给列表中添加列表、元组和字典:

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'}]

3. 给列表中添加空元素

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment