Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Basic Indexing | Indexing and Slicing
Ultimate NumPy
course content

Conteúdo do Curso

Ultimate NumPy

Ultimate NumPy

1. NumPy Basics
2. Indexing and Slicing
3. Commonly used NumPy Functions
4. Math with NumPy

book
Basic Indexing

Every NumPy array has elements and their respective indices. Here, we will focus on indices in 1D arrays. In the following image, the positive indices are shown in green, while the negative indices are shown in red:

As you can see, every element in the array has both a positive and a negative index. In fact, indexing in arrays is similar to indexing in lists.

Accessing Elements by Indices

To access an element by its index, you should specify the index of this element in square brackets, e.g., array[2].

Note

If a specified index is out of bounds, an IndexError is thrown, so be cautious of that.

1234567891011121314
import numpy as np array = np.array([9, 6, 4, 8, 10]) # Accessing the first element (positive index) print(f'The first element (positive index): {array[0]}') # Accessing the first element (negative index) print(f'The first element (negative index): {array[-5]}') # Accessing the last element (positive index) print(f'The last element (positive index): {array[4]}') # Accessing the last element (negative index) print(f'The last element (negative index): {array[-1]}') # Accessing the third element (positive index) print(f'The third element (positive index): {array[2]}') # Accessing the third element (negative index) print(f'The third element (negative index): {array[-3]}')
copy

In fact, positive and negative indexing are just two methods for accessing array elements, and they work the same way functionally.

Note

It is common practice to access the first element of the array using a positive index (0) and the last element using a negative index (-1).

Since the elements of our array are just numbers, we can perform all kinds of operations on them that we would do with regular numbers:

1234
import numpy as np array = np.array([9, 6, 4, 8, 10]) # Finding the average between the first and the last element print((array[0] + array[-1]) / 2)
copy

Here, we calculated the average of the first and the last elements of our array.

To summarize, indexing is essential for accessing, modifying, or extracting specific elements or subsets of data, enabling efficient and precise manipulation of array contents.

Tarefa
test

Swipe to show code editor

Calculate the average of the first, fourth, and last elements:

  1. Use a positive index to access the first element.
  2. Use a positive index to access the fourth element.
  3. Use a negative index to access the last element.
  4. Calculate the average of these numbers.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 1
toggle bottom row

book
Basic Indexing

Every NumPy array has elements and their respective indices. Here, we will focus on indices in 1D arrays. In the following image, the positive indices are shown in green, while the negative indices are shown in red:

As you can see, every element in the array has both a positive and a negative index. In fact, indexing in arrays is similar to indexing in lists.

Accessing Elements by Indices

To access an element by its index, you should specify the index of this element in square brackets, e.g., array[2].

Note

If a specified index is out of bounds, an IndexError is thrown, so be cautious of that.

1234567891011121314
import numpy as np array = np.array([9, 6, 4, 8, 10]) # Accessing the first element (positive index) print(f'The first element (positive index): {array[0]}') # Accessing the first element (negative index) print(f'The first element (negative index): {array[-5]}') # Accessing the last element (positive index) print(f'The last element (positive index): {array[4]}') # Accessing the last element (negative index) print(f'The last element (negative index): {array[-1]}') # Accessing the third element (positive index) print(f'The third element (positive index): {array[2]}') # Accessing the third element (negative index) print(f'The third element (negative index): {array[-3]}')
copy

In fact, positive and negative indexing are just two methods for accessing array elements, and they work the same way functionally.

Note

It is common practice to access the first element of the array using a positive index (0) and the last element using a negative index (-1).

Since the elements of our array are just numbers, we can perform all kinds of operations on them that we would do with regular numbers:

1234
import numpy as np array = np.array([9, 6, 4, 8, 10]) # Finding the average between the first and the last element print((array[0] + array[-1]) / 2)
copy

Here, we calculated the average of the first and the last elements of our array.

To summarize, indexing is essential for accessing, modifying, or extracting specific elements or subsets of data, enabling efficient and precise manipulation of array contents.

Tarefa
test

Swipe to show code editor

Calculate the average of the first, fourth, and last elements:

  1. Use a positive index to access the first element.
  2. Use a positive index to access the fourth element.
  3. Use a negative index to access the last element.
  4. Calculate the average of these numbers.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 1
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt