创新互联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电脑。


网站题目:创新互联Python教程:python默认索引是什么
文章网址:http://www.zjjierui.cn/article/cdcehho.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款