Python Math cos() Method
Python Math cos() Returns the cosine of the radian of x.
Python Math cos() Syntax
Here is the syntax of the cos() method:
import math
math.cos(x)
Note: cos() is not directly accessible, you need to import the math module and then call the method via the math static object.
Python Math cos() Parameters
- x : A value.
Python Math cos() Return Value
Returns the cosine of the radian of x, between -1 and 1.
Python Math cos() Example#1
The following shows an example of using the cos() method.
#!/usr/bin/python3
import math
print ("cos(33) : ", math.cos(33))
print ("cos(-33) : ", math.cos(-33))
print ("cos(0) : ", math.cos(0))
print ("cos(math.pi) : ", math.cos(math.pi))
print ("cos(3*math.pi) : ", math.cos(3*math.pi))
Output:
Python Math cos() Example#2
Python program showing the graphical representation of the cos() function.
import math
import numpy as np
import matplotlib.pyplot as plt
in_array = np.linspace(-(2 * np.pi), 2 * np.pi, 20)
out_array = []
for i in range(len(in_array)):
out_array.append(math.cos(in_array[i]))
i += 1
print("in_array : ", in_array)
print("\nout_array : ", out_array)
plt.plot(in_array, out_array, color = 'red', marker = "o")
plt.title("math.cos() in apidemos.com")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()
Output:
deepinout@ubuntu:~/apidemos.com$ python3 demos.py
in_array : [-6.28318531 -5.62179738 -4.96040945 -4.29902153 -3.6376336 -2.97624567
-2.31485774 -1.65346982 -0.99208189 -0.33069396 0.33069396 0.99208189
1.65346982 2.31485774 2.97624567 3.6376336 4.29902153 4.96040945
5.62179738 6.28318531]
out_array : [1.0, 0.7891405093963934, 0.2454854871407988, -0.40169542465296987, -0.8794737512064891, -0.9863613034027223, -0.6772815716257412, -0.08257934547233249, 0.5469481581224268, 0.9458172417006346, 0.9458172417006346, 0.5469481581224268, -0.0825793454723316, -0.6772815716257405, -0.9863613034027223, -0.8794737512064893, -0.40169542465296987, 0.2454854871407988, 0.7891405093963934, 1.0]