Most versatile compound data type in Python is List. Lists may contain items of different types or same type. Lists can be indexed and sliced. All slice operations return a new list containing the requested elements.
I am creating a list of numbers:
We can retrieve any element in list by referring to its index.
We can return multiple elements of list too. Syntax of this slicing will be listName[startingIndex:endingIndex], this will return elements from startingIndex to endingIndex (will exclude element at endingIndex).
Not providing startingIndex take 0 as starting position by default. Similarly by default n will be taken as endingIndex. If both were absent then entire list will be returned without slicing.
What if the index provided is not available at all? It will throw IndexError:list index out of range
So far slicing is done in sequence order of elements. What if we want to retrieve alternative elements in list. This can be done using slicing. Syntax is list[startIndex:endingIndex:Step].
So far the retrieval is from left to right positioning of list which is convention retrieval. Beauty of python slicing is it can print list in reverse. Can slice elements in reverse order(right to left) too.
[1,2,3,4,5,6,7]Indexing from right to left is
[-n,-n+1,...,-1]In this example its going to be
[-7,-6,-5,-4,-3,-2,-1]
Now the reverse retrieval of entire list using slicing concept with step number in syntax.
To avoid confusion if step number is negative its from right to left. In the absence of step number or its positive then it will be left to right which is traditional retrieval.