python中的索引规则

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Marvel__Dead/article/details/82930827

python中的索引很有趣,它可以用于列表、元組、字符串。

在列表list = [1, 2, 3, 4, 5, 6, 7 ]中 print ("list[1:5]: %s " % list2[1:5]) 结果为 list[1:5]: [2, 3, 4, 5] 即在数组中第一个元素的索引值为0以此类推,其索引的特征是包含起点,但是不包含结束的索引值,所以实际输出结果为1到4号元素。执行print (list2[:-1]) 输出结果为 [1, 2, 3, 4, 5, 6] 即从开始到倒数第二位的所有值。此外python中还有一套索引表示方法:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Marvel__Dead/article/details/82930827
今日推荐