Course Content
NumPy in a Nutshell
NumPy in a Nutshell
Mathematical Operations with 1-D Arrays
With 1-dimensional arrays, you can also perform various mathematical operations. You can subtract, add, multiply, and divide one-dimensional arrays. However, to do this, your arrays must have the same number of elements.
Let's see how we can add two 1-dimensional arrays:
import numpy as np # Creating array arr1 = np.array([5, 7, 3, 1]) arr2= np.array([4, 1, 0, 6]) result_arr = arr1 + arr2 # Displaying array print(result_arr)
If you run the code above, you will obtain the following array: [9, 8, 3, 7]
. How was it created? Through the following operations: 5+4=9, 7+1=8, 3+0=3, 1+6=7
.
Following this example, it's evident that if the number of elements in the first array does not match the number of elements in the second array, it will be impossible to perform addition, subtraction, division, or multiplication between them.
Task
- Create two 1-D arrays:
- the first one should contain the following values:
10, 15, 20, 30
; - the second one should contain these values:
7, 3, 1, 9
.
- the first one should contain the following values:
- Based on the arrays you've created, you should obtain the third array, which should contain the following values:
-3, -12, -19, -21
.
If you're uncertain about how to obtain the third array, please refer to the hint.
Thanks for your feedback!
Mathematical Operations with 1-D Arrays
With 1-dimensional arrays, you can also perform various mathematical operations. You can subtract, add, multiply, and divide one-dimensional arrays. However, to do this, your arrays must have the same number of elements.
Let's see how we can add two 1-dimensional arrays:
import numpy as np # Creating array arr1 = np.array([5, 7, 3, 1]) arr2= np.array([4, 1, 0, 6]) result_arr = arr1 + arr2 # Displaying array print(result_arr)
If you run the code above, you will obtain the following array: [9, 8, 3, 7]
. How was it created? Through the following operations: 5+4=9, 7+1=8, 3+0=3, 1+6=7
.
Following this example, it's evident that if the number of elements in the first array does not match the number of elements in the second array, it will be impossible to perform addition, subtraction, division, or multiplication between them.
Task
- Create two 1-D arrays:
- the first one should contain the following values:
10, 15, 20, 30
; - the second one should contain these values:
7, 3, 1, 9
.
- the first one should contain the following values:
- Based on the arrays you've created, you should obtain the third array, which should contain the following values:
-3, -12, -19, -21
.
If you're uncertain about how to obtain the third array, please refer to the hint.
Thanks for your feedback!
Mathematical Operations with 1-D Arrays
With 1-dimensional arrays, you can also perform various mathematical operations. You can subtract, add, multiply, and divide one-dimensional arrays. However, to do this, your arrays must have the same number of elements.
Let's see how we can add two 1-dimensional arrays:
import numpy as np # Creating array arr1 = np.array([5, 7, 3, 1]) arr2= np.array([4, 1, 0, 6]) result_arr = arr1 + arr2 # Displaying array print(result_arr)
If you run the code above, you will obtain the following array: [9, 8, 3, 7]
. How was it created? Through the following operations: 5+4=9, 7+1=8, 3+0=3, 1+6=7
.
Following this example, it's evident that if the number of elements in the first array does not match the number of elements in the second array, it will be impossible to perform addition, subtraction, division, or multiplication between them.
Task
- Create two 1-D arrays:
- the first one should contain the following values:
10, 15, 20, 30
; - the second one should contain these values:
7, 3, 1, 9
.
- the first one should contain the following values:
- Based on the arrays you've created, you should obtain the third array, which should contain the following values:
-3, -12, -19, -21
.
If you're uncertain about how to obtain the third array, please refer to the hint.
Thanks for your feedback!
With 1-dimensional arrays, you can also perform various mathematical operations. You can subtract, add, multiply, and divide one-dimensional arrays. However, to do this, your arrays must have the same number of elements.
Let's see how we can add two 1-dimensional arrays:
import numpy as np # Creating array arr1 = np.array([5, 7, 3, 1]) arr2= np.array([4, 1, 0, 6]) result_arr = arr1 + arr2 # Displaying array print(result_arr)
If you run the code above, you will obtain the following array: [9, 8, 3, 7]
. How was it created? Through the following operations: 5+4=9, 7+1=8, 3+0=3, 1+6=7
.
Following this example, it's evident that if the number of elements in the first array does not match the number of elements in the second array, it will be impossible to perform addition, subtraction, division, or multiplication between them.
Task
- Create two 1-D arrays:
- the first one should contain the following values:
10, 15, 20, 30
; - the second one should contain these values:
7, 3, 1, 9
.
- the first one should contain the following values:
- Based on the arrays you've created, you should obtain the third array, which should contain the following values:
-3, -12, -19, -21
.
If you're uncertain about how to obtain the third array, please refer to the hint.