Square each number in Numpy array in Python by R4R Team

Task-
In the given array or matrix, we have to square each elements of the array.

Example-

array is [1 2 3 4]
then square of array will give [1 2 9 16]

Syntax-
array=array**2


program-

#import numpy
import numpy as np

l=[1,2,3,4,5]
arr=np.array(l)
print("Matrix is :",arr)
print("After squaring :",arr**2)

l=[[1,2,3],[4,5,1],[1,1,0]]
arr=np.array(l)
print("nMatrix is :n",arr)
print("After squaring :n",arr**2)


output-

Matrix is : [1 2 3 4 5]
After squaring : [ 1 4 9 16 25]

Matrix is :
[[1 2 3]
[4 5 1]
[1 1 0]]
After squaring :
[[ 1 4 9]
[16 25 1]
[ 1 1 0]]

-In this program, we create a array from the list then square each and every element of the array and display them.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!