take the cosine of a number in python

To take the cosine of a number in Python, you first need to import the math module. Then, you can use the cosine() function from the math module to calculate the cosine of the given number.

main.py
import math

# take the cosine of 0.5
cos_val = math.cos(0.5)

print(cos_val)  # output: 0.8775825618903728
108 chars
7 lines

In this example, the math.cos() function takes a single argument, which is the number whose cosine we want to find. The function returns the cosine of the given number, which is stored in the cos_val variable. Finally, we print the value of cos_val, which gives us the result of the cosine calculation.

gistlibby LogSnag