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

  1. math.sin(x) - Returns the sine of x (where x is in radians).
    • Example:

      python
      import math
      x = math.sin(math.pi / 2)
      print(x)  # Output: 1.0
      
  2. math.cos(x) - Returns the cosine of x (where x is in radians).
    • Example:

      python
      import math
      x = math.cos(0)
      print(x)  # Output: 1.0
      
  3. math.tan(x) - Returns the tangent of x (where x is in radians).
    • Example:

      python
      import math
      x = math.tan(math.pi / 4)
      print(x)  # Output: 1.0
      
  4. math.asin(x) - Returns the arc sine of x.
    • Example:

      python
      import math
      x = math.asin(1)
      print(x)  # Output: 1.5707963267948966 (which is π/2)
      
  5. math.acos(x) - Returns the arc cosine of x.
    • Example:

      python
      import math
      x = math.acos(1)
      print(x)  # Output: 0.0
      
  6. math.atan(x) - Returns the arc tangent of x.
    • Example:

      python
      import math
      x = math.atan(1)
      print(x)  # Output: 0.7853981633974483 (which is π/4)
      
  7. math.atan2(y, x) - Returns the arc tangent of y / x.
    • Example:

      python
      import math
      x = math.atan2(1, 1)
      print(x)  # Output: 0.7853981633974483 (which is π/4)
      
  8. math.dist(p, q) - Returns the Euclidean distance between two points p and q.
    • Example:

      python
      import math
      p = (0, 0)
      q = (3, 4)
      distance = math.dist(p, q)
      print(distance)  # Output: 5.0
      
  9. math.hypot(*coordinates) - Returns the Euclidean norm.
    • Example:

      python
      import math
      x = math.hypot(3, 4)
      print(x)  # Output: 5.0
      

Try It Yourself: Fun Exercises

  1. Calculate Angles:
    • Use math.sin(), math.cos(), and math.tan() to calculate the sine, cosine, and tangent of various angles.
  2. Find Distances:
    • Use math.dist() to calculate the distance between different points.
  3. Explore Arc Functions:
    • Experiment with math.asin(), math.acos(), and math.atan() to understand how they work with different values.

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!