Slice notation in Python

Python

Slice notation in Python

#create list
#index [0,1,2,3] #for ref below
a =    [5,6,17,20]
#a[start:stop]  # items start through stop-1
a[2:4]
[17, 20]
#a[start:]      # items start through the rest of the array
a[2:]
[17, 20]
#a[:stop]       # items from the beginning through stop-1
a[:3]
[5, 6, 17]
#a[:]           # a copy of the whole array
a[:]
[5, 6, 17, 20]

Slice in Python with negative number

a[-1]    # last item in the array
20
a[-2:]   # last two items in the array
[17, 20]
a[:-2]   # everything except the last two items
[5, 6]

You can also adjust the step, defaults to 1

#a[start:stop:step] #here will jump by index of 2 rather than default of one
a[0:4:2]
[5, 17]