Skip to content
Home » Trig Functions in Python: sine, cosine, tangent

Trig Functions in Python: sine, cosine, tangent

Spread the love

Let’s have a look at the basic trig functions. How can you use them in calculations with Python?

The three basic trigonometric functions, or trig functions, as they are often called for brevity, so sine, cosine and tangent, are available in the math module, which you must import.

trig functions

List of Angles

Let’s start by creating a list of angles that we can then work on. Here’s the code:

import math

angles = [n * 45 for n in range(9)]

for angle in angles:
    if angles.index(angle) == 0:
        print(f'{angle}\u00b0 = 0 radians')
    else:
        print(f'{angle}\u00b0 = {angles.index(angle) * 1/4} \u03c0 radians')

So, first I used a list comprehension to create a list of angles. The angles are multiples of 45 degrees, so 0 degrees, 45 degrees, 90 degrees, and so on, up to 360 degrees. In the output we want the angles to be printed in degrees and in radians. You can use the degree and pi symbols defined in Unicode, so 00b0 for the degree symbol and 00c0 for pi.

Let’s start by just printing out the angles. If you run this program, you will get the following output:

0° = 0 radians
45° = 0.25 π radians
90° = 0.5 π radians
135° = 0.75 π radians
180° = 1.0 π radians
225° = 1.25 π radians
270° = 1.5 π radians
315° = 1.75 π radians
360° = 2.0 π radians

Now we can print the values of the sine function for these angles. Before we do that, let’s refactor our code a bit so that it’s slightly more concise:

import math

angles = [n * 45 for n in range(9)]

for angle in angles:
    degrees = f'{angle}\u00b0'
    radians = '0 radians' if angles.index(angle) == 0 else f'{angles.index(angle) * 1/4} \u03c0 radians'        
    print(f'{degrees} = {radians}')

If you run this program now, the output will be the same as before.

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Trig Functions in Action

And now let’s expand the code so that it prints the values of the three trig functions, sine, cosine and tangent, for each of the angles.

Here’s the code:

import math

angles = [n * 45 for n in range(9)]

for angle in angles:
    degrees = f'{angle}\u00b0'
    radians = '0 radians' if angles.index(angle) == 0 else f'{angles.index(angle) * 1/4} \u03c0 radians'
        
    print(f'x = {degrees} = {radians}')
    print(f'sin(x) = {round(math.sin(math.radians(angle)), 3)}')
    print(f'cos(x) = {round(math.cos(math.radians(angle)), 3)}')
    if angle not in [90, 270]:
        print(f'tan(x) = {round(math.tan(math.radians(angle)), 3)}')
    print()

You may have noticed that the tangent is not calculated for two angles in our list, 90 and 270 degrees. This is because it’s undefined for these angles.

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

As the trig functions in Python take arguments in radians, I used the radians function from the math module to convert the value in degrees to its equivalent in radians. The values are also rounded to three decimal places.

If you now run this program, the output will be:

x = 0° = 0 radians
sin(x) = 0.0
cos(x) = 1.0
tan(x) = 0.0

x = 45° = 0.25 π radians
sin(x) = 0.707
cos(x) = 0.707
tan(x) = 1.0

x = 90° = 0.5 π radians
sin(x) = 1.0
cos(x) = 0.0

x = 135° = 0.75 π radians
sin(x) = 0.707
cos(x) = -0.707
tan(x) = -1.0

x = 180° = 1.0 π radians
sin(x) = 0.0
cos(x) = -1.0
tan(x) = -0.0

x = 225° = 1.25 π radians
sin(x) = -0.707
cos(x) = -0.707
tan(x) = 1.0

x = 270° = 1.5 π radians
sin(x) = -1.0
cos(x) = -0.0

x = 315° = 1.75 π radians
sin(x) = -0.707
cos(x) = 0.707
tan(x) = -1.0

x = 360° = 2.0 π radians
sin(x) = -0.0
cos(x) = 1.0
tan(x) = -0.0

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love

Leave a Reply