首页 > 文章列表 > python默认索引是什么

python默认索引是什么

python默认索引
188 2022-08-07

1、概念

未指定起始索引时,默认为0;未指定终止索引时,默认为列表长度。

>>> li = ['one', 'two', 'three', 'four', 'five']
>>> li[:4] # 未指定起始索引,默认为 0
# ['one', 'two', 'three', 'four']
>>> li[1:] # 未指定结束索引,默认为 列表长度
# ['two', 'three', 'four', 'five']

2、当前后索引都省略时,意味着创建一份原列表的副本

>>> li = ['one', 'two', 'three', 'four', 'five']
>>> li[:]
# ['one', 'two', 'three', 'four', 'five']

3、注意,切片是原列表的一份副本,操作切片并不会对原列表产生影响

>>> li = ['one', 'two', 'three', 'four', 'five']
>>> li_slice = li[:]
>>> li_slice.append('six')
>>> li_slice
# ['one', 'two', 'three', 'four', 'five', 'six']
>>> li
# ['one', 'two', 'three', 'four', 'five']

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。