Home » Python

math.cos() method with example in Python

Python math.cos() method: Here, we are going to learn about the math.cos() method with example in Python.
Submitted by IncludeHelp, on April 21, 2019

Python math.cos() method

math.cos() method is a library method of math module, it is used to get the cosine of the number radians, it accepts a number returns the cosine of the given number radians.

Note: math.cos() method accepts the only number, if we provide anything else except the number, it returns error TypeError - "TypeError: a float is required".

Syntax of math.cos() method:

    math.cos(x)

Parameter(s): x – is the number whose cosine to be calculated.

Return value: float – it returns a float value that is the cosine value of the number x radians.

Example:

    Input:
    a = 0.278

    # function call
    print(math.cos(a))

    Output:
    0.9616062271291891

Python code to demonstrate example of math.cos() method

# python code to demonstrate example of 
# math.cos() method 

# importing math module
import math

# number
a = -1
print("cos(",a,") is = ", math.cos(a))

a = 0
print("cos(",a,") is = ", math.cos(a))

a = 0.278
print("cos(",a,") is = ", math.cos(a))

a = 1
print("cos(",a,") is = ", math.cos(a))

Output

cos( -1 ) is =  0.5403023058681398
cos( 0 ) is =  1.0
cos( 0.278 ) is =  0.9616062271291891
cos( 1 ) is =  0.5403023058681398

TypeError example

# python code to demonstrate example of 
# math.cos() method with an exception

# importing math module
import math

# number
a = "2"
print("cos(",a,") is = ", math.cos(a))

Output

Traceback (most recent call last):
  File "/home/main.py", line 9, in <module>
    print("acos(",a,") is = ", math.acos(a))
TypeError: a float is required


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.