文章目录
一、列表的格式
列表是由一对中括号包裹起来的元素,称为列表。为可变数据类型,列表中的每一个数据称为元素。可以使用下标来获取元素和对元素切片吧。
1、定义列表的格式:[元素1,元素2, …, 元素n]
2、t = [ ]
3、t = list(可迭代对象)
列表中的元素可以是不同的数据类型
二、列表的操作
2.1 增加元素
append方法是在列表的最后增加一个元素,列表的地址在列表增加或删除前后是不变的。
2.1.1 append方法
append源码解释:
def append(self, *args, **kwargs): # real signature unknown
"""
Append a single item to the end of the bytearray.
item
The item to be appended.
"""
pass
示例代码:
heros = ['阿珂', '嬴政', '韩信', '露娜', '后羿', '亚瑟', '李元芳']
print(id(heros))
# append增加元素, 在列表的最后面追加一个元素
heros.append('黄忠')
print(heros)
print(id(heros))
# 输出结果:
['阿珂', '嬴政', '韩信', '露娜', '后羿', '亚瑟', '李元芳']
1806544791560
['阿珂', '嬴政', '韩信', '露娜'