How to calculate the secant in Python
To calculate the secant in Python, you can use the sec() function of the mpmath (math) library.
from mpmath import sec
sec(x)
The input parameter x is the measure of the angle in radians.
The function returns the secant of the angle.
What is the secant? In trigonometry, the secant is the segment that joins the origin with the end point of the tangent (OD). The secant is the inverse of the cosine. The secant ranges from zero (0 °) to infinity (90 °).
Alternative method
The secant can also be calculated as the inverse of the cosine.
from math import cos
print(1/(cos(x))
Examples
Example 1
To calculate the secant of 35 °
from math import radians
from mpmath import sec
x=radians(35)
sec(x)
The radians() function converts the angle from sexagesimal to radians.
The sec() function calculates the secant.
mpf('1.2207745887614561')
The secant of the 35 ° angle is approximately 1.22077
Example 2
This script calculates the secant through equivalence with the cosine.
from math import cos
x=radians(35)
print(1/cos(x))
The output result is
1.220774588761456
It is the same result as the previous example but it is obtained in a different way.