浩晨众云网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

创新互联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://cdjierui.cn/article/cdcehho.html
在线咨询
服务热线
服务热线:028-86922220
TOP