晴川云Python教程:python默认索引是什么

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

以上就是python默认索引的介绍,希望对大家有所帮助。更多Python学习指路:晴川云python教程

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

原创文章,作者:晴川运维,如若转载,请注明出处:https://baike.qcidc.com/6007.html

(0)
晴川运维晴川运维
上一篇 2025年6月8日
下一篇 2025年6月8日

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注