Home » Python

Trigonometric functions in Python

Python Trigonometric functions: Here, we are going to learn about the Trigonometric functions of math module with examples in Python?
Submitted by IncludeHelp, on April 25, 2019

Python Trigonometric functions/methods

In python programming language, there are some of the built-in functions which are defined in math module – they can be used for Trigonometric calculations, python has following Trigonometric functions, which are used to various purposes.

List of Trigonometric functions in Python

Trigonometric functions Description Example
math.cos() It returns the cosine of the number (in radians). math.cos(x)
math.sin() It returns the sine of the number (in radians). math.sin(x)
math.tan() It returns the tangent of the number in radians. math.tan(x)
math.acos() It returns the arc cosine of the number in radians. math.acos(x)
math.asin() It returns the arc sine of the number in radians. math.asin(x)
math.atan() It returns the arc tangent of the number in radians. math.atan(x)
math.atan2() It returns the result of the atan(y/x). math.atan2(y,x)
math.pypot() Return the Euclidean norm, sqrt(x*x + y*y). math.pypot(x,y)

Python code to demonstrate example of all Trigonometric functions

# Python code to demonstrate example 
# of all Trigonometric functions

# importing math module
import math

# number 
x = 0.75 

# math.cos()
print("math.cos(",x,"): ", math.cos(x));
# math.sin()
print("math.sin(",x,"): ", math.sin(x));
# math.tan()
print("math.tan(",x,"): ", math.tan(x));

# math.acos()
print("math.acos(",x,"): ", math.acos(x));
# math.asin()
print("math.asin(",x,"): ", math.asin(x));
# math.atan()
print("math.atan(",x,"): ", math.atan(x));

y = 2
# math.atan2(y,x) = atan(y/x)
print("math.atan2(",y,",",x,"): ", math.atan2(y,x))

# math.hypot(x,y)
print("math.hypot(",x,",",y,"): ", math.hypot(x,y))

Output

math.cos( 0.75 ):  0.7316888688738209
math.sin( 0.75 ):  0.6816387600233341
math.tan( 0.75 ):  0.9315964599440725
math.acos( 0.75 ):  0.7227342478134157
math.asin( 0.75 ):  0.848062078981481
math.atan( 0.75 ):  0.6435011087932844
math.atan2( 2 , 0.75 ):  1.2120256565243244
math.hypot( 0.75 , 2 ):  2.1360009363293826

Exceptions with Trigonometric functions

There are two types of exceptions occur,

  • ValueError
    When we provide an invalid value (number), this exception occurs.
  • TypeError
    When we provide a different type of values except a number, this exception occurs.

ValueError example

# python code to demonstrate example of 
# math.asin() method with an exception

# importing math module
import math

# number
a = 2
print("asin(",a,") is = ", math.asin(a))

Output

Traceback (most recent call last):
  File "/home/main.py", line 9, in <module>
    print("asin(",a,") is = ", math.asin(a))
ValueError: math domain error

TypeError example

# python code to demonstrate example of 
# math.cos() method with an exception

# importing math module
import math

# number
a = "2"
print("cos(",a,") is = ", math.cos(a))

Output

Traceback (most recent call last):
  File "/home/main.py", line 9, in <module>
    print("acos(",a,") is = ", math.acos(a))
TypeError: a float is required


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.