In the previous article, we have discussed Python Program for sin() Function
tan() Function in Python:
The tangent of a number is returned by the math.tan() method.
The tangent function’s mathematical formula is:
tan(A) = length of opposite side/length of adjacent side
Syntax:
math.tan(x)
Parameters:
x: This is Required. It is the number to calculate the tangent of. If the value is not a number, a TypeError is returned.
Return Value:
Returns a float value representing a number’s tangent.
- The tan function returns the Tangent value if the number argument is positive or negative.
- The tan function returns TypeError if the number argument is not a number.
Examples:
Example1:
Input:
Given first number (angle) = 60 Given second number (angle) = 45
Output:
The result after applying tan() function on above given first number 60 = 0.320040389379563 The result after applying tan() function on above given second number 45 = 1.6197751905438615
Example2:
Input:
Given first number (angle) = 90 Given second number (angle) = 15
Output:
The result after applying tan() function on above given first number 90 = -1.995200412208242 The result after applying tan() function on above given second number 15 = -0.8559934009085187
Program for tan() 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.tan() function to the given first number to get the tangent value of a given number (angle).
- Store it in another variable.
- Similarly, do the same for the other number.
- Apply math.tan() 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 = [5, 15, 6, 7] # Give the first number (angle) as static input and store it in another variable. gvn_numb1 = 60 # Apply math.tan() function to the given first number to get the tangent value # of a given number (angle). # Store it in another variable. fnl_rslt = math.tan(gvn_numb1) print("The result after applying tan() 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 tan() function on above given second number", gvn_numb2, " = ", math.tan(gvn_numb2)) # Apply math.tan() function to the given list element and print it. print( "The result after applying tan() function on given list element gvnlst[1] = ", math.tan(gvn_lst[1]))
Output:
The result after applying tan() function on above given first number 60 = 0.320040389379563 The result after applying tan() function on above given second number 45 = 1.6197751905438615 The result after applying tan() function on given list element gvnlst[1] = -0.8559934009085187
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 (angle) as user input using the float(input()) function and store it in a variable.
- Apply math.tan() function to the given first number to get the tangent value of a given number (angle).
- Store it in another variable.
- Similarly, do the same for the other number.
- Apply math.tan() 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(angle) as user input using the float(input()) function # and store it in a variable. gvn_numb1 = float(input("Enter some random number = ")) # Apply math.tan() function to the given first number to get the tangent value # of a given number (angle). # Store it in another variable. fnl_rslt = math.tan(gvn_numb1) print("The result after applying tan() 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 tan() function on above given second number", gvn_numb2, " = ", math.tan(gvn_numb2)) # Apply math.tan() function to the given list element and print it. print( "The result after applying tan() function on given list element gvnlst[2] = ", math.tan(gvn_lst[2]))
Output:
Enter some random List Elements separated by spaces = 10 20 30 40 50 Enter some random number = 90 The result after applying tan() function on above given first number 90.0 = -1.995200412208242 Enter some random number = 65 The result after applying tan() function on above given second number 65.0 = -1.4700382576631723 The result after applying tan() function on given list element gvnlst[2] = -6.405331196646276
Know all about Mathematical Functions in Python by going through our Python Mathematical Methods Examples well explained step by step.