w3resource

NumPy: Compute sine, cosine and tangent array of angles given in degrees

NumPy Mathematics: Exercise-21 with Solution

Write a NumPy program to compute the trigonometric sine, cosine and tangent array of angles given in degrees.

Sample Solution:

Python Code:

import numpy as np
print("sine: array of angles given in degrees")
print(np.sin(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.))
print("cosine: array of angles given in degrees")
print(np.cos(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.))
print("tangent: array of angles given in degrees")
print(np.tan(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.))

Sample Output:

sine: array of angles given in degrees                                 
[ 0.          0.5         0.70710678  0.8660254   1.        ]          
cosine: array of angles given in degrees                               
[  1.00000000e+00   8.66025404e-01   7.07106781e-01   5.00000000e-01   
   6.12323400e-17]                                                     
tangent: array of angles given in degrees                              
[  0.00000000e+00   5.77350269e-01   1.00000000e+00   1.73205081e+00   
   1.63312394e+16]

Pictorial Presentation:

NumPy Mathematics: Compute sine, cosine and tangent array of angles given in degrees.
NumPy Mathematics: Compute sine, cosine and tangent array of angles given in degrees.
NumPy Mathematics: Compute sine, cosine and tangent array of angles given in degrees.

Python Code Editor:

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

Previous: Write a NumPy program to create a random array with 1000 elements and compute the average, variance, standard deviation of the array elements.

Next: Write a NumPy program to calculate inverse sine, inverse cosine, and inverse 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+++