Convert angles from degrees to radians with Python deg2rad()

PythonNumpyServer Side ProgrammingProgramming

To convert a degree array to radians, use the numpy.deg2rad() method in Python Numpy. The method returns the corresponding angle in radians. This is a scalar if x is a scalar. The 1st parameter is an input angle in degrees. The 2nd and 3rd parameters are optional.

The 2nd parameter is an ndarray, A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.

The 3rd parameter is the condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value.

Steps

At first, import the required library −

import numpy as np

Create an Array −

arr = np.arange(12.)

Displaying our array −

print("Array...\n",arr)

Get the datatype −

print("\nArray datatype...\n",arr.dtype)

Get the dimensions of the Array −

print("\nArray Dimensions...\n",arr.ndim)

Get the number of elements of the Array −

print("\nNumber of elements in the Array...\n",arr.size)

Degree array −

res = arr*30

To convert a degree array to radians, use the numpy.deg2rad() method in Python Numpy. The method returns the corresponding angle in radians. This is a scalar if x is a scalar −

print("\nDegree array to degrees...\n",np.deg2rad(res))

Example

import numpy as np

# Create an Array
arr = np.arange(12.)

# Display the array
print("Array...\n", arr)

# Get the type of the array
print("\nOur Array type...\n", arr.dtype)

# Get the dimensions of the Array
print("\nOur Array Dimensions...\n",arr.ndim)

# Get the number of elements in the Array
print("\nNumber of elements...\n", arr.size)

# Degree array
res = arr*30

# To convert a degree array to radians, use the numpy.deg2rad() method in Python Numpy
# The method returns the corresponding angle in radians. This is a scalar if x is a scalar.
print("\nDegree array to degrees...\n",np.deg2rad(res))

Output

Array...
[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.]

Our Array type...
float64

Our Array Dimensions...
1

Number of elements...
12

Degree array to degrees...
[0. 0.52359878 1.04719755 1.57079633 2.0943951 2.61799388
3.14159265 3.66519143 4.1887902 4.71238898 5.23598776 5.75958653]
raja
Updated on 28-Feb-2022 06:22:30

Advertisements