Python Practice › NumPy Programs › Program 71
Square every element
Write a NumPy program to print the squares of all elements.
Input
[2, 6, 3, 1]
Output
[ 4 36 9 1]
Approach
a ** 2ornp.square.
Solution
import numpy as npprint(np.square(np.array([2, 6, 3, 1])))Output
[ 4 36 9 1]
Learn the concept
New to this topic? Read the lesson, then come back.