logo

Python Number Cos() Method


Show

Description

The cos() method in Python number returns the cosine of x radians. In Python, the math module comprises a number of mathematical operations that can be performed easily and the cos() function is one of them. It returns the cosine of the value that has been passed as an argument. The value that is passed in this function must be in radians.

Syntax

Here is the syntax for cos() method:

cos(x)

Note - One cannot access this function directly, so it is required to import the math module in Python, and then we are supposed to call this function making use of the math static object.

Parameters

  • x - This should be a numeric value.

Return Value

This method in the Python math module returns a numeric value that lies between -1 and 1, which represents the cosine of the angle.

Example

The example below shows the usage of cos() method.

#!/usr/bin/python
import math

print "cos(3) : ",  math.cos(3)
print "cos(-3) : ",  math.cos(-3)
print "cos(0) : ",  math.cos(0)
print "cos(math.pi) : ",  math.cos(math.pi)
print "cos(2*math.pi) : ",  math.cos(2*math.pi)

When we run the above program, it produces the following result:

cos(3) :  -0.9899924966
cos(-3) :  -0.9899924966
cos(0) :  1.0
cos(math.pi) :  -1.0
cos(2*math.pi) :  1.0

Here at Intellinuts, we have created a complete Python tutorial for Beginners to get started in Python.