Course Content
Ultimate NumPy
Ultimate NumPy
Basic Indexing
Every NumPy array has elements and their respective indices. Here we will focus on indices in 1D arrays. Let's take a look at the following image:
Here we have an array of five elements: [9, 6, 4, 8, 10]
. Every element has a positive and negative index. For example, 9
has 0
as its positive index and -5
as its negative index. As you can see, positive indexing in arrays starts at 0
and goes up to n-1
, where n
is the length of the array. Negative indexing, on the other hand, starts at -n
and goes up to -1
.
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.
Let's take a look at an example:
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]}')
As you can see, there is nothing complicated here.
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:
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)
Here, we calculated the average of the first and the last elements of our array.
Swipe to show code editor
Calculate the average of the first, fourth, and last elements:
- Use a positive index to access the first element.
- Use a positive index to access the fourth element.
- Use a negative index to access the last element.
- Calculate the average of these numbers.
Thanks for your feedback!
Basic Indexing
Every NumPy array has elements and their respective indices. Here we will focus on indices in 1D arrays. Let's take a look at the following image:
Here we have an array of five elements: [9, 6, 4, 8, 10]
. Every element has a positive and negative index. For example, 9
has 0
as its positive index and -5
as its negative index. As you can see, positive indexing in arrays starts at 0
and goes up to n-1
, where n
is the length of the array. Negative indexing, on the other hand, starts at -n
and goes up to -1
.
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.
Let's take a look at an example:
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]}')
As you can see, there is nothing complicated here.
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:
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)
Here, we calculated the average of the first and the last elements of our array.
Swipe to show code editor
Calculate the average of the first, fourth, and last elements:
- Use a positive index to access the first element.
- Use a positive index to access the fourth element.
- Use a negative index to access the last element.
- Calculate the average of these numbers.
Thanks for your feedback!
Basic Indexing
Every NumPy array has elements and their respective indices. Here we will focus on indices in 1D arrays. Let's take a look at the following image:
Here we have an array of five elements: [9, 6, 4, 8, 10]
. Every element has a positive and negative index. For example, 9
has 0
as its positive index and -5
as its negative index. As you can see, positive indexing in arrays starts at 0
and goes up to n-1
, where n
is the length of the array. Negative indexing, on the other hand, starts at -n
and goes up to -1
.
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.
Let's take a look at an example:
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]}')
As you can see, there is nothing complicated here.
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:
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)
Here, we calculated the average of the first and the last elements of our array.
Swipe to show code editor
Calculate the average of the first, fourth, and last elements:
- Use a positive index to access the first element.
- Use a positive index to access the fourth element.
- Use a negative index to access the last element.
- Calculate the average of these numbers.
Thanks for your feedback!
Every NumPy array has elements and their respective indices. Here we will focus on indices in 1D arrays. Let's take a look at the following image:
Here we have an array of five elements: [9, 6, 4, 8, 10]
. Every element has a positive and negative index. For example, 9
has 0
as its positive index and -5
as its negative index. As you can see, positive indexing in arrays starts at 0
and goes up to n-1
, where n
is the length of the array. Negative indexing, on the other hand, starts at -n
and goes up to -1
.
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.
Let's take a look at an example:
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]}')
As you can see, there is nothing complicated here.
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:
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)
Here, we calculated the average of the first and the last elements of our array.
Swipe to show code editor
Calculate the average of the first, fourth, and last elements:
- Use a positive index to access the first element.
- Use a positive index to access the fourth element.
- Use a negative index to access the last element.
- Calculate the average of these numbers.