Latest Code Tutorials

Python Math cos() Function Example

Python cos() is an inbuilt method defined under the math module, which is used to find the cosine value of the parameter passed in radians. We can use the math module by importing it. The syntax for it would be import math; after importing, we use to call this function using the static object.

Python math cos()

The math.cos() function returns the cosine of the value passed as an argument. The value passed in the cos() function should be in radians. The Math.cos() method returns the numeric value between -1 and 1, which represents the cosine of the angle given in radians. The cos() is the static method of Math; therefore, it is always used as Math.




It takes one parameter var, which takes values of numeric datatype and throws type error if the argument is of any other data type is passed.

Return Value

It returns the cosine value of the number in the float datatype.

Example programs on cos() method in Python:

See the following code.

x = 0.36



Let’s go for a more descriptive example.

Write a program to show the working of the cos() method in Python.


import math

a1 = 0.36
b1 = 1
c1 = -1
d1 = -0.36

print("Value for parameter ", a1, " is ", math.cos(a1))
print("Value for parameter ", b1, " is ", math.cos(b1))
print("Value for parameter ", c1, " is ", math.cos(c1))
print("Value for parameter ", d1, " is ", math.cos(d1))


Value for parameter  0.36  is  0.9358968236779348
Value for parameter  1  is  0.5403023058681398
Value for parameter  -1  is  0.5403023058681398
Value for parameter  -0.36  is  0.9358968236779348

In this example, we have seen that by passing a valid parameter which is different for different examples we get the desired cos() method solution which is the cosine value of the parameter passed.

Example 2: Write a program to pass a value out of range from the cos() function and display the output.


import math

a = 'b'


TypeError: must be real number, not str

In this example, we can see that when we passed a value other than the real number it throws a Type Error.

Graphical Representation of Python cos()

See the following code example in which we present the value of cos function in graphical format.

For that, we will use numpy, matplotlib libraries.


import math
import numpy as np
import matplotlib.pyplot as plt

input_arr = np.linspace(-(2 * np.pi), 2 * np.pi, 20)

out_arr = []

for i in range(len(input_arr)):
    i += 1

print("input_arr : ", input_arr)
print("\nout_arr : ", out_arr)

plt.plot(input_arr, out_arr, color='red', marker="o")


Python Math cos()

input_arr :  [-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_arr :  [1.0, 0.7891405093963935, 0.24548548714079876, -0.40169542465296987, -0.8794737512064891, -0.9863613034027223, -0.6772815716257412, -0.0825793454723325, 0.5469481581224268, 0.9458172417006346, 0.9458172417006346, 0.5469481581224268, -0.08257934547233162, -0.6772815716257405, -0.9863613034027223, -0.8794737512064893, -0.40169542465296987, 0.24548548714079876, 0.7891405093963935, 1.0]

In the above code, we are pointing the graph of cos with different values based on four quadrants.

The value of cos is between -1 to +1. So we have plotted all the values between -1 to +1.


Cosine is one of the basic trigonometric ratios. To find out cos or cosine in Python we use the math.cos() function.

See also

Python fabs()

Python sqrt()

Python frexp()

Python copysign()

Python isinf()

Python Idexp()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.