Home > Article > Backend Development > What do positive and negative indexes mean in Python?
With indexes, we can access items in the Python sequence data type. Strings, lists, tuples, and range objects are sequence data types. In this tutorial, we'll cover indexing in detail.
Linear data structures in any programming language are built around indexes. The default index for each machine starts at 0 and goes up to n-1. In this case, n represents the total number of items in the corresponding data structure. Types include
Positive indexing − Increases from 0 to 1.
Negative indexing − each traversal moves from tail to head, starting with the last element.
These facilitate our ability to access the many components of this data structure. Let's examine the procedures in the next part.
Similar to how we access elements in lists and strings, we can also access elements in tuples. Therefore, indexing and slicing are the only methods we need to access elements. Furthermore, indexing is intuitive, starting at index zero, just like a list. Furthermore, the number we put in square brackets represents the index of the tuple. Let's look at some examples of using tuple indexing to retrieve tuple elements.
The Chinese translation oftup1 = (10, 3, 4, 22, 1) # for accessing the first element of the tuple print(tup1[0])
10
tup1 = (10, 3, 4, 22, 1) # accessing the third element of the tuple print(tup1[2])
4The Chinese translation of
tup1 = (10, 3, 4, 22, 1) print(tup1[10]) # gives an error as the index is only up to 4
IndexError: tuple index out of range
tup1 = (10, 3, 4, 22, 1) print(tup1[1+3]) # the expression inside the square brackets results in an integer index 4. Hence, we get the element at the 4th index.
1
In Python, positive zero-based indexing is the fundamental method for accessing iterable items.
As a result, an index starting at 0 may refer to any element in the iterable.
The first zero-based indexed element has an index of 0, the second one has an index of 1, and so on.
myList = [1, 2, 3, 4, 5] # NORMAL INDEXING print(myList[0]) print(myList[1]) print(myList[2]) # NEGATIVE INDEXING (STARTS FROM THE LAST ELEMENT IN THE LIST) print(myList[-1]) print(myList[-2]) print(myList[-3]) print(myList[-3:])
1 2 3 5 4 3 [3, 4, 5]
If we wish to print the components starting at the end, we may also use negative indexes. Additionally, by specifying the index number with a negative sign, we may carry out negative tuple indexing (-). As a result, this suggests that the compiler starts thinking about the elements in reverse order, beginning with the element that comes last in the tuple.
The Chinese translation oftup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1) print(tup[-4])
2
Now, we can use negative indexes in slicing also.
tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1) print(tup[-4:-1])
(2, 56, 890)
The tuple index() function aids in locating an element's index or location within a tuple. Essentially, this function serves two purposes −
Giving the tuple's first instance of each element.
If the indicated element is absent from the tuple, throwing an error.
tuple.index(value)
Using the pop() function and passing -1 as argument to it, we can remove the last element of the list and get a new list.
my_list = [45, 5, 33, 1, -9, 8, 76] my_list.pop(-1) print(my_list)
[45, 5, 33, 1, -9, 8]
tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1) print(tup.index(45)) print(tup.index(890)) #prints the index of elements 45 and 890
2 7
Requires fewer lines of code and the reversal can be done in one line.
Simplifies difficult processes.
Run quickly while requiring little complexity
In short, Python allows positive indexing starting from zero and negative indexing starting from -1. In Python, negative indexing means that the indexing process starts from the end of the iterable object. The last element is at index -1, the second to last element is at index -2, and so on. Negative indexing of arrays is supported in the Python computer language, but not in most other programming languages. This means that the index value -1 provides the last element of the array and -2 provides the second to last element. Negative indexes start at the end of the array.
(2, 56, 890)The above is the detailed content of What do positive and negative indexes mean in Python?. For more information, please follow other related articles on the PHP Chinese website!