DEV Community

Cover image for Calculating Sine, Cosine and Tangent in Python
Daniel Nogueira
Daniel Nogueira

Posted on

5

Calculating Sine, Cosine and Tangent in Python

Let's calculate sine, cosine and tangent in Python. It is not a difficult task, but it requires attention to some details.

First of all, we need to import the math library:

import math
Enter fullscreen mode Exit fullscreen mode

Next, we need to read the angle value the user will enter. But an important note, the calculation requires the angle value in radians.

So, just perform this conversion from angle to radians. How? Using the radians() method from the math library:

ang = int(input('Angle: '))
rad = math.radians(ang)
Enter fullscreen mode Exit fullscreen mode

With the angle value in radians, we can now calculate the sine, cosine and tangent values. We'll use the sin(), cos() and tan() methods from the math() library:

sine = math.sin(rad)
cosine = math.cos(rad)
tangent = math.tan(rad)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay