Python的数据结构 -- 序列

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)