Python can be used to complete trigonometric calculations. You can calculate sine, cosine, and tangent as well as use other trig functions using Python's math module, which is part of the Python Standard Library and comes included with all Python installations. (This post is adapted from my book Problem Solving with Python.)

The Python REPL

Calculations can be completed at the Python Prompt, also called the Python REPL. REPL stands for Read Evaluate Print Loop. The Python REPL shows three arrow symbols >>> followed by a blinking cursor. Programmers type commands at the >>> prompt then hit [ENTER] to see the results.

Commands typed into the Python REPL are read by the interpreter, results of running the commands are evaluated, then printed to the command window. After the output is printed, the >>> prompt appears on a new line. This process repeats over and over again in a continuous loop.

Alternatively, all of the Python code below can be typed into a Jupyter notebook code cell. The code can be run by clicking the Run button in Jupyter or hitting Shift+Enter.

Trigonometry: sine, cosine, and tangent

Trigonometry functions such as sine, cosine, and tangent can also be calculated using the Python REPL or typed into a Jupyter notebook.

To use Python's trig functions, we need to introduce a concept: importing modules.

In Python, there are many operations built into the language when the REPL or a Jupyter notebook starts. These operations include + , -, *, / like we saw in the previous post. However, not all functions will work right away when Python starts.

Say we want to find the sine of an angle. Try typing the following into the Python REPL or into a Jupyter notebook:

>>> sin(60)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'sin' is not defined

This error results because we have not told Python to include the sin function. The sin function is part of the Python Standard Library. The Python Standard Library comes with every Python installation and includes many functions, but not all of these functions are available to us when we start a new Python REPL session or open up a new Jupyter notebook.

Importing Modules

To use Python's sin function, first import the sin function from the math module which is part of the Python Standard Library.

Importing modules and functions in Python is easy. Use the following syntax:

from module import function

To import the sin() function from the math module try:

>>> from math import sin
>>> sin(60)

Importing Multiple Modules

Multiple modules can be imported at the same time. Say we want to use a bunch of different trig functions to solve the following problem:

An angle has a value of $\pi$/6 radians. What is the sine, cos, and tangent of the angle?

To solve this problem we need to import the sin(), cos(), and tan() functions. It is also useful to have the value of $\pi$, rather than having to write 3.14.... We can import all of these functions at the same time using the syntax:

from module import function1, function2, function3

Note the commas in between the function names.


>>> from math import sin, cos, tan, pi
>>> pi
>>> sin(pi/6)
>>> cos(pi/6)
>>> tan(pi/6)


The following trig functions are part of Python's math module:

Trig function Name Description Example Result
math.pi pi mathematical constant $\pi$ math.pi 3.14
math.sin() sine sine of an angle in radians math.sin(4) 9.025
math.cos() cosine cosine of an angle in radians cos(3.1) 400
math.tan() tangent tangent of an angle in radians tan(100) 2.0
math.asin() arc sine inverse sine, ouput in radians math.sin(4) 9.025
math.acos() arc cosine inverse cosine, ouput in radians log(3.1) 400
math.atan() arc tangent inverse tangent, ouput in radians atan(100) 2.0
math.radians() radians conversion degrees to radians math.radians(90) 1.57
math.degrees() degree conversion radians to degrees math.degrees(2) 114.59


Want to learn more about doing math and calculations with Python? Check out my book Problem Solving with Python on Amazon (Affiliate Link):