Found this great table at Http://wiki.python.org/moin/MovingToPythonFromOtherLanguages
PythonIndexesandSlicesForA six-Element list.IndexesEnumerate the Elements,Slices enumerate the spaces between the elements.Index FromRear: -6 -5 -4 -3 -2 -1A=[0,1,2,3,4,5]A[1:]==[1,2,3,4,5]Index FromFront: 0 1 2 3 4 5Len(A)==6A[:5]==[0,1,2,3,4] +---+---+---+---+---+---+A[0]==0A[:-2]==[0,1,2,3] |A|B|C|D|E|F|A[5]==5A[1:2]==[1] +---+---+---+---+---+---+A[-1]==5A[1:-1]==[1,2,3,4]Slice FromFront: : 1 2 3 4 5 :A[-2]==4Slice FromRear: : -5 -4 -3 -2 -1 :=a[:] B==[ 0,1,2 ,3,4 5] (shallow copy of a< Span class= "pun")
In Python 2.7
Slicing in Python
[A:B:C]
Len = length of string, tuple or list
C--default is +1. Sign of C indicates forward or backward, absolute value of C indicates steps. Default is forward with step size 1. Positive means forward, negative means backward.
A-when the C is positive or blank, the default is 0. When the C is negative, default is-1.
B-When the C is positive or blank, the default is Len. When C was negative, default is-(len+1).
Understanding index assignment is very important.
In forward direction, starts @ 0 and ends at len-1
In backward direction, starts at-1 and ends At-len
When you say [A:B:C] is saying depending on the C (forward or backward), start at A and end at B (excluding Elem ENT at BTH Index). Use the indexing rule above and remember you'll only find elements in this range
-len,-len+1,-len+2, ..., 0, 1, 2,3,4, len-1
But this range continues in both directions infinitely
...,-len-2,-len-1,-len,-len+1,-len+2, ..., 0, 1, 2,3,4, len-1, Len, Len +1, len+2, ....
e.g.
0 1 2 3 4 5 6 7 8 9 10 11 a s t r i n g -9 -8 -7 -6 Span class= "pun" >-5 -4 -3 -2 -1
If your choice of a, B and C allows overlap with the range above as you traverse using rules for a,b,c above you'll EIT She get a list with elements (touched during traversal) or you'll get an empty list.
One last thing:if A and B is equal, then also you get an empty list
>>>l1[2, 3, 4]>>>l1[:][2, 3, 4]>>> L1[::-1]#A default is-1, b default is-(len+1)[4, 3, 2]>>> L1[:-4:-1]#a default is-1[4, 3, 2]>>> L1[:-3:-1]#a default is-1[4, 3]>>> l1[::]#c default is +1, so a default is 0, and b default is Len[2, 3, 4]>>> L1[::-1]#c is-1, so a default is-1 and b default is-(len+1)[4, 3, 2]>>> L1[-100:-200:-1]#Interesting[]>>> L1[-1:-200:-1]#Interesting[4, 3, 2]>>> l1[-1:5:1][4]>>> l1[-1:-1:1][]>>> L1[-1:5:1]#Interesting[4]>>> l1[1:-7:1][]>>> L1[1:-7:-1]#Interesting[3, 2]
/span>
Python Learning--string slicing