列表
lst = ['hello', 'world', 996]
print(id(lst))
print(type(lst))
print(lst)
列表的特点:
- 列表元素按顺序有序排列
- 索引映射唯一个数据
- 列表可以存储重复数据
- 任意数据类型混存
- 根据需要动态分配和回收内存
获取列表中的元素:
正向索引从0到N-1,举例:lst[0]
逆向索引从-N到-1,举例;lst[-N]
指定索引不存在,抛出IndexError
lst = ['hello', 'world', 996, 'hello']
print(lst.index('hello')) #如果列表中有相同元素,只返回列表中相同元素的第一个元素的索引
print(lst.index('hello', 1, 3)) #在1到3的范围中搜索hello,但是不包括3
lst = ['hello', 'world', 996, 'zzz']
print(lst[0])
print(lst[-1])
列表切片操作:
lst = [10, 20, 30, 40, 50, 60, 70, 80, 90]
lst_copy = lst[1:4:1]
print(id(type))
print(type(lst_copy))
print(lst_copy)
lst_copy = lst[1:6:2]
print(lst_copy)