In the previous article, we have discussed Python Program for atan2() Function
cos() Function in Python:
The cosine of a number is returned by the math.cos() method.
The Trigonometry Cosine function’s mathematical formula is
cos(A)= adjacent side of A/ Hypotenuse
Syntax:
math.cos(x)
Parameters:
x: This is required. It is a number. A number for which the cosine must be calculated. If the value is not a number, a TypeError is returned.
Return Value:
Returns a float value ranging from -1 to 1 that represents the cosine of an angle.
- The cos() function returns the Cosine value if the number argument is positive or negative.
- The cos() function returns TypeError if it is not a number.
Examples:
Example1:
Input:
Given first number = 1 Given second number = 45
Output:
The result after applying cos() function on above given first number 1 = 0.5403023058681398 The result after applying cos() function on above given second number 45 = 0.5253219888177297
Example2:
Input:
Given first number = 60 Given second number = 10
Output:
The result after applying cos() function on above given first number 60 = -0.9524129804151563 The result after applying cos() function on above given second number 10 = -0.8390715290764524
Program for cos() Function in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Import math module using the import keyword.
- Give the list as static input and store it in a variable.
- Give the first number (angle) as static input and store it in another variable.
- Apply math.cos() function to the given first number to get the cosine value of a given number.
- Store it in another variable.
- Print the above result.
- Similarly, do the same for the other number.
- Apply math.cos() function to the given list element and print it.
- The Exit of Program.
Below is the implementation:
# Import math module using the import keyword. import math # Give the list as static input and store it in a variable. gvn_lst = [10, 15, 20, 30, 35] # Give the first number as static input and store it in another variable. gvn_numb1 = 1 # Apply math.cos() function to the given first number to get the cosine value # of a given number. # Store it in another variable. fnl_rslt = math.cos(gvn_numb1) print("The result after applying cos() function on above given first number", gvn_numb1, " = ", fnl_rslt) # similarly do the same for the other number. gvn_numb2 = 45 print("The result after applying cos() function on above given second number", gvn_numb2, " = ", math.cos(gvn_numb2)) # Apply math.cos() function to the given list element and print it. print( "The result after applying cos() function on given list element gvnlst[1] = ", math.cos(gvn_lst[1]))
Output:
The result after applying cos() function on above given first number 1 = 0.5403023058681398 The result after applying cos() function on above given second number 45 = 0.5253219888177297 The result after applying cos() function on given list element gvnlst[1] = -0.7596879128588213
Method #2: Using Built-in Functions (User Input)
Approach:
- Import math module using the import keyword.
- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in a variable.
- Give the first number as user input using the float(input()) function and store it in a variable.
- Apply math.cos() function to the given first number to get the cosine value of a given number.
- Store it in another variable.
- Print the above result.
- Similarly, do the same for the other number.
- Apply math.cos() function to the given list element and print it.
- The Exit of Program.
Below is the implementation:
# Import math module using the import keyword. import math # Give the list as user input using list(),map(),input(),and split() functions. # Store it in a variable. gvn_lst = list(map(float, input( 'Enter some random List Elements separated by spaces = ').split())) # Give the first number as user input using the float(input()) function # and store it in a variable. gvn_numb1 = float(input("Enter some random number = ")) # Apply math.cos() function to the given first number to get the cosine value # of a given number. # Store it in another variable. fnl_rslt = math.cos(gvn_numb1) print("The result after applying cos() function on above given first number", gvn_numb1, " = ", fnl_rslt) # similarly do the same for the other number. gvn_numb2 = float(input("Enter some random number = ")) print("The result after applying cos() function on above given second number", gvn_numb2, " = ", math.cos(gvn_numb2)) # Apply math.cos() function to the given list element and print it. print( "The result after applying cos() function on given list element gvnlst[2] = ", math.cos(gvn_lst[2]))
Output:
Enter some random List Elements separated by spaces = 10 15 5 6 0 Enter some random number = 60 The result after applying cos() function on above given first number 60.0 = -0.9524129804151563 Enter some random number = 20.5 The result after applying cos() function on above given second number 20.5 = -0.07956356727854007 The result after applying cos() function on given list element gvnlst[2] = 0.28366218546322625
Know all about Mathematical Functions in Python by going through our Python Mathematical Methods Examples well explained step by step.