Python NumPy rad2deg() Function

NumPy rad2deg() Function:

The degrees() function of the Numpy module is a mathematical function that converts the angles from radians to degrees.

Syntax:

numpy.rad2deg(a, out=None)

Parameters

a: This is required. It is the array or an object given as input.

out: This is optional. It is the location where the result will be stored. 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 array of corresponding degree angles for the radians in the given input array is returned.

NumPy rad2deg() Function in Python

Approach:

  • Import numpy module using the import keyword
  • Import math module using the import keyword
  • Pass some random list as an argument to the array() function
    of the Numpy module to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the above-given array as an argument to the rad2deg() function of the numpy module to convert the angles from radians to degrees for each element of the given array.
  • Store it in another variable.
  • Print the angles in degrees for the given array.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Import math module using the import keyword
import math  
# Pass some random list as an argument to the array() function 
# of the Numpy module to create an array. 
# Store it in a variable.
gvn_arry = np.array([math.pi/3, math.pi/2, math.pi, 0])                     
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
print()
# Pass the above given array as an argument to the rad2deg() function of the 
# numpy module to convert the angles from radians to degrees for each element of
# the given array
# Store it in another variable.  
rslt = np.rad2deg(gvn_arry)   
# Print the angles in degrees for the given array
print ("The angles in degrees for the given array:")  
print(rslt)

Output:

The above given array is:
[1.04719755 1.57079633 3.14159265 0. ]

The angles in degrees for the given array:
[ 60. 90. 180. 0.]