Home  >  Article  >  Backend Development  >  What do positive and negative indexes mean in Python?

What do positive and negative indexes mean in Python?

WBOY
WBOYforward
2023-08-20 19:37:071628browse

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.

What is a list index?

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.

Tuple Indexing

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 of

Example 1

is:

Example 1

tup1 = (10, 3, 4, 22, 1)
# for accessing the first element of the tuple
print(tup1[0])

Output

10

Example 2

tup1 = (10, 3, 4, 22, 1)
# accessing the third element of the tuple
print(tup1[2])

Output

4
The Chinese translation of

Example 3

is:

Example 3

tup1 = (10, 3, 4, 22, 1)
print(tup1[10])
# gives an error as the index is only up to 4

Output

IndexError: tuple index out of range

Example 4

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.

Output

1

Zero-Based Indexing in Python

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.

Example 5

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:])

Output

1
2
3
5
4
3
[3, 4, 5]

Negative index

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 of

Example 3

is:

Example 3

tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1)
print(tup[-4])

Output

2

Now, we can use negative indexes in slicing also.

Example 4

tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1)
print(tup[-4:-1])

Output

(2, 56, 890)

Tuple index() method

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.

Syntax

tuple.index(value)

Example 5: Deleting elements using negative indexes

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)

Output

[45, 5, 33, 1, -9, 8]

Example 6: Finding the index of an element

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

Output

2
7

Advantages of using negative indexes in Python lists

  • Requires fewer lines of code and the reversal can be done in one line.

  • Simplifies difficult processes.

  • Run quickly while requiring little complexity

Conclusion

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete