Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Access Array Elements | Indexing and Slicing
NumPy in a Nutshell
course content

Conteúdo do Curso

NumPy in a Nutshell

NumPy in a Nutshell

1. Getting Started with NumPy
2. Dimensions in Arrays
3. Indexing and Slicing
4. Important Functions

book
Access Array Elements

In both lists and arrays, elements are accessed using square brackets. Let's review the distinction between indexing and slicing:

  • To retrieve a single element, you simply need to specify the index of that element in square brackets (start counting from 0);
  • If you want to obtain a sequence from the original array, you should use slices.

We'll start with simple indexing. Let's have a look at the following image:

Let's see how it works with examples.

Get the first element from the following array:

1234567
import numpy as np # Creating array arr = np.array([1, 2, 3, 4, 5]) # Get the first element print(arr[0])
copy

Retrieve the second element from the following array:

1234567
import numpy as np # Creating array arr = np.array([1, 2, 3, 4, 5]) # Get the second element print(arr[1])
copy

Retrieve the third and fourth elements from the following array and then add them together:

1234567
import numpy as np # Creating array arr = np.array([1, 2, 3, 4, 5]) # Adding the third and the fourth elements print(arr[2] + arr[3])
copy

Now, it's time to explore slicing. First, let's examine the syntax of slicing: array[start:end:step], where:

  • start is the index from which slicing begins;
  • end is the index where slicing stops (note that this index is not included);
  • step is the parameter that specifies the intervals between the indices.

Let's have a look at the following image:

Omitting start, end and step

As you can see, we can often omit the start, end, step or even all of them at the same time. step, for example, can be omitted when we want it to be equal to 1. start and end can be omitted in the following scenarios:

  1. Omitting start:
    • slicing from the first element (step is positive);
    • slicing from the last element (step is negative).
  2. Omitting end:
    • slicing to the last element inclusive (step is positive);
    • slicing to the first element inclusive (step is negative).

In the example above, a[2:4] has the step equal to 1. a[-2:] goes from the second to last element to the end of the array with step equal to 1. a[::2] goes from the first element to the end of the array with step equal to 2.

It's time to practice.

Tarefa
test

Swipe to show code editor

Retrieve the first and last elements from the following array [13, 99, 11, 23, 5, 41] and then multiply them. Please use positive indexing.

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 3. Capítulo 1
toggle bottom row

book
Access Array Elements

In both lists and arrays, elements are accessed using square brackets. Let's review the distinction between indexing and slicing:

  • To retrieve a single element, you simply need to specify the index of that element in square brackets (start counting from 0);
  • If you want to obtain a sequence from the original array, you should use slices.

We'll start with simple indexing. Let's have a look at the following image:

Let's see how it works with examples.

Get the first element from the following array:

1234567
import numpy as np # Creating array arr = np.array([1, 2, 3, 4, 5]) # Get the first element print(arr[0])
copy

Retrieve the second element from the following array:

1234567
import numpy as np # Creating array arr = np.array([1, 2, 3, 4, 5]) # Get the second element print(arr[1])
copy

Retrieve the third and fourth elements from the following array and then add them together:

1234567
import numpy as np # Creating array arr = np.array([1, 2, 3, 4, 5]) # Adding the third and the fourth elements print(arr[2] + arr[3])
copy

Now, it's time to explore slicing. First, let's examine the syntax of slicing: array[start:end:step], where:

  • start is the index from which slicing begins;
  • end is the index where slicing stops (note that this index is not included);
  • step is the parameter that specifies the intervals between the indices.

Let's have a look at the following image:

Omitting start, end and step

As you can see, we can often omit the start, end, step or even all of them at the same time. step, for example, can be omitted when we want it to be equal to 1. start and end can be omitted in the following scenarios:

  1. Omitting start:
    • slicing from the first element (step is positive);
    • slicing from the last element (step is negative).
  2. Omitting end:
    • slicing to the last element inclusive (step is positive);
    • slicing to the first element inclusive (step is negative).

In the example above, a[2:4] has the step equal to 1. a[-2:] goes from the second to last element to the end of the array with step equal to 1. a[::2] goes from the first element to the end of the array with step equal to 2.

It's time to practice.

Tarefa
test

Swipe to show code editor

Retrieve the first and last elements from the following array [13, 99, 11, 23, 5, 41] and then multiply them. Please use positive indexing.

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 3. 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