

If a required parameter is not provided by us, then Python will set the parameter with default values. Here we have set the start_index to be 2 and step_length to be 1. Here is another example of the usage of Slice Notation (), this time on strings Example#2: Slice Notation () and Strings > string1 = 'ant-man' In other words, python starts collecting values from the given sequence from start_index, moves through the list by incrementing the index using the step_length and stops once reaching the stop_index and returns the collected values!

In line-2, a portion/slice of this list is extracted using the slice notation x. In line-1, x is initialized as a list object with 5 items: ‘a’, ‘b’, ‘c’, ‘d’ and ‘e’.
#Keyclick notation code#
Let us see how the code in the above example works. (On a side note, if you are looking for an example with the syntax x or similar syntax, scroll down to the section “ The variants of using Slice Notation” below) Example#1: Slice Notation and Lists > x = Python’s “” notation, officially known as Slice Notation is used to extract the desired portion/slice from a given sequence. In this article let us learn about the commonly used “” notation in Python, and learn how we can wield the power of this notation in our programs.įor those of you in a hurry, here is the short version of the answer.
