python -- squence index and slice

#test for seq
lsShop = ['apple', 'mango', 'carrot', 'banana'];

#index operation
nLen = len(lsShop);
for i in range(-2, nLen):
    print "item %d is %s\n" %(i, lsShop[i]);

#slice operation
nSubLen = len(lsShop[0]);
strItem = lsShop[0];
for i in range(-3, nSubLen + 1):
    print "%s slice %d  ==> %s\n" %(strItem, i, strItem[0:i]);

#another test
nListLen = len(lsShop);
for i in range(-3, nListLen):
    print "%d  ==> %s\n" %(i, lsShop[0:i]);

 

你可能感兴趣的:(apple,python)