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:
1234567import numpy as np # Creating array arr = np.array([1, 2, 3, 4, 5]) # Get the first element print(arr[0])
Retrieve the second element from the following array:
1234567import numpy as np # Creating array arr = np.array([1, 2, 3, 4, 5]) # Get the second element print(arr[1])
Retrieve the third and fourth elements from the following array and then add them together:
1234567import 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])
Now, it's time to explore slicing. First, let's examine the syntax of slicing: array[start:end:step], where:
startis the index from which slicing begins;endis the index where slicing stops (note that this index is not included);stepis 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:
- Omitting
start:- slicing from the first element (
stepis positive); - slicing from the last element (
stepis negative).
- slicing from the first element (
- Omitting
end:- slicing to the last element inclusive (
stepis positive); - slicing to the first element inclusive (
stepis negative).
- slicing to the last element inclusive (
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.
Swipe to start coding
Retrieve the first and last elements from the following array [13, 99, 11, 23, 5, 41] and then multiply them. Please use positive indexing.
Løsning
Tak for dine kommentarer!
single
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Opsummér dette kapitel
Explain code
Explain why doesn't solve task
Awesome!
Completion rate improved to 4.76
Access Array Elements
Stryg for at vise menuen
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:
1234567import numpy as np # Creating array arr = np.array([1, 2, 3, 4, 5]) # Get the first element print(arr[0])
Retrieve the second element from the following array:
1234567import numpy as np # Creating array arr = np.array([1, 2, 3, 4, 5]) # Get the second element print(arr[1])
Retrieve the third and fourth elements from the following array and then add them together:
1234567import 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])
Now, it's time to explore slicing. First, let's examine the syntax of slicing: array[start:end:step], where:
startis the index from which slicing begins;endis the index where slicing stops (note that this index is not included);stepis 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:
- Omitting
start:- slicing from the first element (
stepis positive); - slicing from the last element (
stepis negative).
- slicing from the first element (
- Omitting
end:- slicing to the last element inclusive (
stepis positive); - slicing to the first element inclusive (
stepis negative).
- slicing to the last element inclusive (
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.
Swipe to start coding
Retrieve the first and last elements from the following array [13, 99, 11, 23, 5, 41] and then multiply them. Please use positive indexing.
Løsning
Tak for dine kommentarer!
single