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

What do positive and negative indexes mean in Python?

WBOY
Release: 2023-08-20 19:37:07
forward
1666 people have browsed it

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])
Copy after login

Output

10
Copy after login

Example 2

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

Output

4
Copy after login
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
Copy after login

Output

IndexError: tuple index out of range
Copy after login

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.
Copy after login

Output

1
Copy after login

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:])
Copy after login

Output

1
2
3
5
4
3
[3, 4, 5]
Copy after login

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])
Copy after login

Output

2
Copy after login

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])
Copy after login

Output

(2, 56, 890)
Copy after login

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)
Copy after login

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)
Copy after login

Output

[45, 5, 33, 1, -9, 8]
Copy after login

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
Copy after login

Output

2
7
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template