What is a Negative Indexing in Python?Introduction:In this tutorial, we are learning the Negative Indexing in Python. It has many features and capabilities. One of these features is negative indexing. It allows you to access the contents of a section (such as a list, string, or tuple) from the end by using the negative index measure. What is the Negative Indexing?Indexing is a method of accessing an element or items. It is array based on their position or order in the sequence. For example, if you have a list of numbers and you can use indexing to retrieve the first, second, third, or other items in the list. Indexing in Python is zero-based. Program Code: Here, we give a program code for the Indexing in Python. The code is given below - Output: Now we run the above code and find the value from it. The output is given below - The second element of the given list is: 20 The third element of the given list is: 34 The fourth element of the given list is: 48 The fifth element of the given list is: 56 The sixth element of the given list is: 67 Program Code: Here we give a program code of the negative Indexing in Python. The code is given below - Output: Now we run the above code and find the value from it. The output is given below - The last element of the given list is: 67 The second last element of the given list is: 56 The third last element of the given list is: 48 The fourth last element of the given list is: 34 The fifth last element of the given list is: 20 Working of the negative indexing in Python:Python adds the length of the sequence to the negative index to obtain the positive index. For example, if you have a list of five elements and you use index -1, Python will add 5 (length of the list) to -1 (index) and get 4 (positive index). It will then return the contents of the 3rd position in the list. In general, if you have an array of n elements and use the -i index (where i is a positive number), then Python will add n (length of the array) to -i (index) and get n-i (positive index). It then returns the element of position n-i in a row. Negative indexing is a simple way to access an element from the end of an array without having to calculate its positive index. Using the negative indexing in Python:In Python, you can use negative indexing on any sequence type, such as list, string, or tuple. Here, we discuss some examples of negative indexing in Python. Program code 1: Here we give a program code to find the last element of a sequence by using the negative indexing in Python. We can use the -1 index to get the last element of the given sequence. The code is given below - Output: Now, we run the above code and find the last element of all the given sequences. The output is given below - The last element of the list is: 34 The last character of the string is: d The last element of the tuple is: 18 The last element of the range is: 24 Program code 2: Here we give a program code to find the second last element of a sequence by using the negative indexing in Python. We can use the -2 index to get the second last element of the given sequence. The code is given below - Output: Now, we run the above code and find the second last element of all the given sequences. The output is given below - The second last element of the list is: 23 The second last character of the string is: l The second last element of the tuple is: 27 The second last element of the range is: 23 Program code 3: Here we give a program code to find the first element from the end of a sequence by using the negative indexing in Python. We can use the -len (range) option to get the first element at the end of a given sequence. This is equivalent to using index 0 to get the first element at the beginning of the sequence. The code is given below - Output: Now, we run the above code and find the first element from the end of all sequences. The output is given below - The first element from the end of the list is: 10 The first character from the end of the string is: H The first element from the end of the tuple is: 34 The first element from the end of the range is: 0 Program code 4: Here, we give a program code for slicing by using negative indexing in Python. We can reverse any section using the slice with negative indexing. Slicing is a way to get a subsequence or part of an array by specifying a starting index, ending index, and an optional step size. The syntax for slicing is string [start:end:step]. If any of these parameters are omitted, Python uses the default values. The code is given below - Output: Now, we run the above code and find the element from the sequence. Here, we can see the slicing with a negative index and step size -1 reverses the order. This is because it starts from the last element (-1) and moves backward until it reaches the first element (-len(sequence)) with a step size of -1. The output is given below - [10, 12, 23, 34] [34, 23, 12, 10] [34, 23, 12, 10] Conclusion:Here we are learning Negative Indexing in Python. This is important for obtaining the last few elements of a given sequence and backtracking a sequence. It can also be used to perform other tasks that require access to the endpoint. Here, we also learn some program code for negative indexing using Python. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India