The Python cos function is one of the Math functions which calculates the Trigonometry Cosine for the specified expression. The cos function returns the value between -1 and 1, and in this section, we discuss how to use this math function with an example.
The mathematical formula behind the Trigonometry Cosine function is
COS(x) = Length of the Adjacent Side / Length of the Hypotenuse
The syntax of the cos Function in Python Programming Language is as shown below.
math.cos(number);
Number: It can be a number or a valid numerical expression for which you want to find the Cosine value.
- If the number argument is a positive or negative number, the cos function returns the Cosine value.
- If it is not a number, this function returns TypeError.
Python cos Function Example
The cos Function allows finding the trigonometric cosine for the numeric values. In this example, we are going to find the Cosine values of different data types and display the output. Please refer Python acos method article from our Python basics page to find the Arc Cosine of the specified expression.
First, We declared Python List and Python Tuple with some random values. Next, we used this cos method directly on both positive and negative integers. The first two statements find the Cosine of the corresponding values.
Next, We used the cosine Function on Tuple and List items. The above screenshot shows that the function is working perfectly on them.
Next, we used the cos() function from the list of math methods on multiple values. Within the last statement, we tried it on the String value, and it returns a TypeError.
TIP: Please refer to the Python sin and Python tan methods from the list of Python math functions to find the sine and tangent values.
import math
Tup = (1, 2, 3, -4 , 5)
Lis = [-1, 2, -3.5, -4 , 5]
print('Cosine value of Positive Number = %.2f' %math.cos(10))
print('Cosine value of Negative Number = %.2f' %math.cos(-15))
print('Cosine value of Tuple Item = %.2f' %math.cos(Tup[3]))
print('Cosine value of List Item = %.2f' %math.cos(Lis[2]))
print('Cosine value of Multiple Number = %.2f' %math.cos(10 + 20 - 40))
print('Cosine value of String Number = ', math.cos('Hello'))
