Trigonometric Functions
What You'll Learn: In this tutorial, you'll discover various trigonometric functions available in Python's math module. These functions help you perform calculations involving angles and distances.
Trigonometric Functions
math.sin(x)- Returns the sine of x (where x is in radians).Example:
pythonimport math x = math.sin(math.pi / 2) print(x) # Output: 1.0
math.cos(x)- Returns the cosine of x (where x is in radians).Example:
pythonimport math x = math.cos(0) print(x) # Output: 1.0
math.tan(x)- Returns the tangent of x (where x is in radians).Example:
pythonimport math x = math.tan(math.pi / 4) print(x) # Output: 1.0
math.asin(x)- Returns the arc sine of x.Example:
pythonimport math x = math.asin(1) print(x) # Output: 1.5707963267948966 (which is π/2)
math.acos(x)- Returns the arc cosine of x.Example:
pythonimport math x = math.acos(1) print(x) # Output: 0.0
math.atan(x)- Returns the arc tangent of x.Example:
pythonimport math x = math.atan(1) print(x) # Output: 0.7853981633974483 (which is π/4)
math.atan2(y, x)- Returns the arc tangent of y / x.Example:
pythonimport math x = math.atan2(1, 1) print(x) # Output: 0.7853981633974483 (which is π/4)
math.dist(p, q)- Returns the Euclidean distance between two points p and q.Example:
pythonimport math p = (0, 0) q = (3, 4) distance = math.dist(p, q) print(distance) # Output: 5.0
math.hypot(*coordinates)- Returns the Euclidean norm.Example:
pythonimport math x = math.hypot(3, 4) print(x) # Output: 5.0
Try It Yourself: Fun Exercises
- Calculate Angles:
- Use
math.sin(),math.cos(), andmath.tan()to calculate the sine, cosine, and tangent of various angles.
- Use
- Find Distances:
- Use
math.dist()to calculate the distance between different points.
- Use
- Explore Arc Functions:
- Experiment with
math.asin(),math.acos(), andmath.atan()to understand how they work with different values.
- Experiment with
Summary:
In this Python tutorial, we explored various trigonometric functions available in the math module. These functions help you perform calculations involving angles and distances. Keep experimenting and have fun with Python!