Python math.cos() function


The math.cos() function in Python is used to calculate the cosine of a given angle. Similar to the sine function, the angle must be provided in radians, and the function returns the cosine value as a float. The cosine function is a fundamental mathematical function in trigonometry, which finds applications in physics, engineering, and computer graphics.

Syntax

import math result = math.cos(x)
  • x: The angle in radians for which you want to calculate the cosine.

Return Value

  • Returns the cosine of the angle x as a float. The result will be in the range [1,1][-1, 1].

Examples

  1. Calculating the cosine of an angle in radians:

    import math result = math.cos(math.pi) # Cosine of 180 degrees (π radians) print(result) # Output: -1.0
  2. Calculating the cosine of other angles:

    result = math.cos(0) # Cosine of 0 degrees (0 radians) print(result) # Output: 1.0 result = math.cos(math.pi / 2) # Cosine of 90 degrees (π/2 radians) print(result) # Output: 0.0 result = math.cos(-math.pi / 2) # Cosine of -90 degrees (-π/2 radians) print(result) # Output: 0.0
  3. Using math.cos() with angles in degrees: If you have an angle in degrees, you can convert it to radians using math.radians() before calculating the cosine:

    angle_deg = 60 result = math.cos(math.radians(angle_deg)) # Cosine of 60 degrees print(result) # Output: 0.5000000000000001 (approximately 1/2)
  4. Using math.cos() in expressions: You can use math.cos() in more complex calculations:

    angle_rad = math.pi / 4 # 45 degrees result = math.cos(angle_rad) ** 2 + math.sin(angle_rad) ** 2 print(result) # Output: 1.0 (because cos(45)^2 + sin(45)^2 = 1)

Summary

  • The math.cos() function computes the cosine of an angle in radians.
  • The result is always a float and lies within the range of [1,1][-1, 1].
  • This function is essential in trigonometry and is widely used in various applications, including physics, engineering, and computer graphics, where waveforms and periodic functions are involved.