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)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series