Python Practice › NumPy Programs › Program 71

Square every element

EasyNumPy Programs · 71 of 100

Write a NumPy program to print the squares of all elements.

Advertisement
Input
[2, 6, 3, 1]
Output
[ 4 36  9  1]

Approach

  1. a ** 2 or np.square.

Solution

Python
import numpy as npprint(np.square(np.array([2, 6, 3, 1])))
Output
[ 4 36  9  1]
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs