0% found this document useful (0 votes)
3 views2 pages

Array

The document provides a series of exercises for using NumPy in Python, including creating 1D and 2D arrays, performing arithmetic operations like addition and subtraction, and calculating the sum of elements. It demonstrates element-wise operations between two arrays and matrix multiplication. Each exercise includes code snippets and expected output.

Uploaded by

Arnab Mondal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Array

The document provides a series of exercises for using NumPy in Python, including creating 1D and 2D arrays, performing arithmetic operations like addition and subtraction, and calculating the sum of elements. It demonstrates element-wise operations between two arrays and matrix multiplication. Each exercise includes code snippets and expected output.

Uploaded by

Arnab Mondal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

19/09/2024, 22:28 Copy of Welcome To Colab - Colab

Name: Arghyadip Sahu ID: 221003003001

1. Create a 1D NumPy array and assign values. Then print the values.

import numpy as np
array = np.array([1,2,3,4,5])
print(array)

[1 2 3 4 5]

2. Create a 2D NumPy array and assign values. Then print the values.

import numpy as np
array_2d = np.array([[ 1,2,3],
[4,5,6],
[7,8,9]])
print(array_2d)

[[1 2 3]
[4 5 6]
[7 8 9]]

3. Add a constant value to all elements of a 2D NumPy array.

import numpy as np
array_2d = np.array([[1,2,3],
[4,5,6],
[7,8,9]])
constant = 10
array_2d_updated=array_2d + constant
print(array_2d_updated)

[[11 12 13]
[14 15 16]
[17 18 19]]

4. Subtract a constant value to all elements of a 2D NumPy array.

import numpy as np
array_2d=np.array([[1,2,3],
[4,5,6],
[7,8,9]])
Constant = 10
array_2d_updated = array_2d-constant
print(array_2d_updated)

[[-9 -8 -7]
[-6 -5 -4]
[-3 -2 -1]]

5. Calculate the sum of all elements of a 2D NumPy array.

import numpy as np
array_2d=np.array([[1,2,3],
[4,5,6],
[7,8,9]])
total_sum=np.sum(array_2d)
print(total_sum)

45

6. Perform element wise sum between two 2D NumPy arrays.

https://colab.research.google.com/drive/1NdbbG8ZXBIu9q1vBWE1D9OkIHNqGkbRs?usp=classroom_web#scrollTo=8gpxV5n2lsMF&printMode=true 1/2
19/09/2024, 22:28 Copy of Welcome To Colab - Colab
import numpy as np
array1 = np.array([[1,2,3],[4,5,6],[7,8,9]])
array2 = np.array([[10,11,12],[13,14,15],[16,17,18]])
sum_array = array1+array2
sum_array = np.add(array1 , array2)
print(sum_array)

[[11 13 15]
[17 19 21]
[23 25 27]]

7. Perform element wise subtraction between two 2D NumPy arrays.

import numpy as np
array1 = np.array([[1,2,3],[4,5,6],[7,8,9]])
array2 = np.array([[10,11,12],[13,14,15],[16,17,18]])
subtract_array=array1 - array2
subtract_array=np.subtract(array1 , array2)
print(subtract_array)

[[-9 -9 -9]
[-9 -9 -9]
[-9 -9 -9]]

8. Perform matrix multiplication between two 2D NumPy arrays.

import numpy as np
array1 = np.array([[1,2,3],[4,5,6],[7,8,9]])
array2 = np.array([[10,11,12],[13,14,15],[16,17,18]])
multiply_array=array1 * array2
multiply_array=np.multiply(array1 , array2)
print(multiply_array)

[[ 10 22 36]
[ 52 70 90]
[112 136 162]]

https://colab.research.google.com/drive/1NdbbG8ZXBIu9q1vBWE1D9OkIHNqGkbRs?usp=classroom_web#scrollTo=8gpxV5n2lsMF&printMode=true 2/2

You might also like