w3resource

NumPy: Convert angles from degrees to radians for all elements in a given array

NumPy Mathematics: Exercise-24 with Solution

Write a NumPy program to convert angles from degrees to radians for all elements in a given array.

Sample Input: Input: [-180., -90., 90., 180.]

Sample Solution:-

Python Code:

import numpy as np
x = np.array([-180.,  -90.,   90.,  180.])
r1 = np.radians(x)
r2 = np.deg2rad(x)
assert np.array_equiv(r1, r2)
print(r1)

Sample Output:

[-3.14159265 -1.57079633  1.57079633  3.14159265]

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to convert angles from radians to degrees for all elements in a given array.

Next: Write a NumPy program to calculate hyperbolic sine, hyperbolic cosine, and hyperbolic tangent for all elements in a given array.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Getting rid of unwanted characters:

You can get rid of whitespaces or any specific character using strip methods in Python. You can use either plain strip for both sides, lstrip for the left side and rstrip for the right side only.

str="+++Welcome python+++"
str.strip()

print(str)

Output:

+++Welcome python+++