How to calculate the sine of an angle in python ?

Created September 01, 2022 | Viewed 301 | by Benjamin Edit


Examples of how to get the sine of an angle in python

Using python math module

To calculate the sine of an angle in python, a solution is to use the python math module:

import math

angle = 0.7 # angle in radians

math.sin(angle)

returns

0.644217687237691

Another example

angle = 0. # angle in radians

math.sin(angle)

returns

0.0

Note to get pi number value:

math.pi

returns

3.141592653589793

Example:

angle = math.pi

math.sin(angle)

returns

1.2246467991473532e-16

Or

angle = math.pi / 2.0 # angle in radians

math.sin(angle)

returns

1.0

Convert degrees to radians

If angles are in degrees, it is possible to convert them in radians using math

angle = 90
angle = math.radians(angle)

returns

1.5707963267948966

and

math.sin(angle)

returns

 1.0

Sine for a list of angles

angles = [0,math.pi/3.0,math.pi/2.0,math.pi]

returns

[0, 1.0471975511965976, 1.5707963267948966, 3.141592653589793]

A solution is to use a list comprehension

[math.sin(angle) for angle in angles]

returns

[0.0, 0.8660254037844386, 1.0, 1.2246467991473532e-16]

Sine for an array of angles

import numpy as np

A = np.array([0, 1.0471975511965976, 1.5707963267948966, 3.141592653589793])

returns

array([0.        , 1.04719755, 1.57079633, 3.14159265])

then

np.sin(A)

returns

array([0.00000000e+00, 8.66025404e-01, 1.00000000e+00, 1.22464680e-16])

References

Links Site
cosine docs.python.org
degrees docs.python.org