Python Math – Get cosine of an angle

In this post we will be creating a Python script that will calculate the cosine of any given angle. The script will take a number via input and find the cosine before printing to output. To achieve this we will be using the math module, which provides access to the mathematical functions defined by the C standard.

See the snippet of Python code below which uses the ‘cos()’ function, the function will calculate the cosine of the number given and return this to output.

import math

while True:
    Number=float(input("Please Enter Number to find Cosine: "))
    print(math.cos(Number))

An example of the code being run can be seen below.

Please Enter Number to find Cosine: 2
-0.4161468365471424
Please Enter Number to find Cosine: 90
-0.4480736161291701
Please Enter Number to find Cosine: 42
-0.39998531498835127
>>>

Take a look at some of our other content around the Python programming language by clicking here.

Leave a Reply