31 Mar Scalar operations on Numpy arrays
Scalar operations on Numpy arrays include performing addition or subtraction, or multiplication on each element of a Numpy array. Let us see the following examples:
- Addition Operation on Numpy Arrays
- Subtraction Operation on Numpy Arrays
- Multiplication Operation on Numpy Arrays
- Division Operation on Numpy Arrays
Addition Operation on Numpy Arrays
The Addition Operation is adding a value to each element of a NumPy array. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import numpy as np # Create two arrays n1 = np.array([15, 20, 25, 30]) n2 = np.array([65, 75, 85, 95]) print("Array1 =", n1) print("Array2 =", n2) # performing addition on each element n1 = n1 + 10; n2 = n2 + 20; print("Array1 (updated) ", n1) print("Array2 (updated) ", n2) |
Output
1 2 3 4 5 6 |
Array1 = [15 20 25 30] Array2 = [65 75 85 95] Array1 (updated) [25 30 35 40] Array2 (updated) [ 85 95 105 115] |
Subtraction Operation on Numpy Arrays
The Subtraction operation is subtracting a value from each element of a NumPy array. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import numpy as np # Create two Numpy arrays n1 = np.array([15, 20, 25, 30]) n2 = np.array([65, 75, 85, 95]) print("Array1 =", n1) print("Array2 =", n2) # performing subtraction on each element n1 = n1 - 5; n2 = n2 - 10; print("Array1 (updated) ", n1) print("Array2 (updated) ", n2) |
Output
1 2 3 4 5 6 |
Array1 = [15 20 25 30] Array2 = [65 75 85 95] Array1 (updated) [10 15 20 25] Array2 (updated) [55 65 75 85] |
Multiplication Operation on Numpy Arrays
The Multiplication operation is multiplying a value to each element of a NumPy array. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import numpy as np # Create two arrays n1 = np.array([15, 20, 25, 30]) n2 = np.array([65, 75, 85, 95]) print("Array1 =", n1) print("Array2 =", n2) # performing multiplication on each element n1 = n1 * 5; n2 = n2 * 10; print("Array1 (updated) ", n1) print("Array2 (updated) ", n2) |
Output
1 2 3 4 5 6 |
Array1 = [15 20 25 30] Array2 = [65 75 85 95] Array1 (updated) [ 75 100 125 150] Array2 (updated) [650 750 850 950] |
Division Operation on Numpy Arrays
The Division operation is to divide a value from each element of a NumPy array. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import numpy as np # Create two arrays n1 = np.array([15, 20, 25, 30]) n2 = np.array([65, 75, 85, 95]) print("Array1 =", n1) print("Array2 =", n2) # performing division on each element n1 = n1 / 5; n2 = n2 / 5; print("Array1 (updated) ", n1) print("Array2 (updated) ", n2) |
Output
1 2 3 4 5 6 |
Array1 = [15 20 25 30] Array2 = [65 75 85 95] Array1 (updated) [3. 4. 5. 6.] Array2 (updated) [13. 15. 17. 19.] |
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments