Np.sqrt in python – Python NumPy sqrt() Function

NumPy sqrt() Function:

Np.sqrt in python: The square root of a specified number is calculated using the sqrt() function of the NumPy module.

Syntax:

numpy.sqrt(x, out=None)

Parameters

x: This is required. It is an array (array-like) having elements for which the square root values are calculated.

out: This is optional. It is the location where the result will be saved. It must have a shape that the inputs broadcast to if it is provided. If None or not given, a newly allocated array is returned.

Return Value: 

The square root value of each element of x is returned.

NumPy sqrt() Function in Python

Example1

Approach:

  • Import numpy module using the import keyword.
  • Pass some random list as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the above given array as an argument to the sqrt() function of the numpy module to get the square root values of given array elements.
  • Store it in another variable.
  • Print the square root values of given array elements.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([16, 44, 25, 225, 100])
# Pass the above given array as an argument to the sqrt() function of the 
# numpy module to get the square root values of given array elements
# Store it in another variable.
rslt = np.sqrt(gvn_arry)
# Print the square root values of given array elements 
print("The square root values of given array elements = ")
print(rslt)

Output:

The square root values of given array elements = 
[ 4.  6.63324958  5.  15.  10. ]

Example2 – (For Complex Numbers)

Approach:

  • Import numpy module using the import keyword.
  • Pass some random list with complex numbers as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the above given array as an argument to the sqrt() function of the numpy module to get the square root values of given complex array elements
  • Store it in another variable.
  • Print the square root values of given complex array elements
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list with complex numbers as an argument to the 
# array() function to create an array. 
# Store it in a variable.
gvn_arry = np.array([2, -3, -5 + 3J])
# Pass the above given array as an argument to the sqrt() function of the 
# numpy module to get the square root values of given complex array elements
# Store it in another variable.
rslt = np.sqrt(gvn_arry)
# Print the square root values of given complex array elements 
print("The square root values of given array elements with complex values = ")
print(rslt)

Output:

The square root values of given array elements with complex values = 
[1.41421356+0.j  0.  +1.73205081j   0.64457424+2.32711752j]