码迷,mamicode.com
首页 > 编程语言 > 详细

Python的数据结构 -- 序列

时间:2015-04-22 17:35:12      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

shoppingList = [apple,banana,cat,dog,duck]

#Indexing and ‘subscription operation‘
print(The first item is,shoppingList[0])
print(The second item is,shoppingList[1])
print(the third item is,shoppingList[2])
print(item -1 is,shoppingList[-1])
print(item -2 is,shoppingList[-2])

#Slicing on a list
print(Form the 1 to 3 is,shoppingList[1:3])
print(From the 2 to 4 is,shoppingList[2:4])
print(Form the 1 to -1 is,shoppingList[2:-3])
print(From the start to end is,shoppingList[:])

#Slicing on a String
name=keritafranklin
print(From the 1 to 3 is,name[1:3])
print(From the 2 to 4 is,name[2:4])
print(From the 1 to -1 is,name[1:-1])
print(From the start to end is,name[:])

#序列的特性:
#1、索引操作符
#2、切片操作符
#  -1指的是最后一个、-2指的是倒数第二个。以此类推
#  如果在某个区间没有元素,则返回[]
#  如果用这样引用[:],则表示输出所有元素
#  切片时从第一个序列开始,到第二个序列前截止

2015/4/21

Python的数据结构 -- 序列

标签:

原文地址:http://www.cnblogs.com/kerita/p/4447924.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!