Python angle conversion function

Python Angle Conversion Functions

degrees() Function

The degrees() method converts radians x to degrees.

Syntax

The syntax of the degrees() method is as follows:

degrees(x)

Note − This function cannot be accessed directly, so we need to import the math module and then call it using the math static object.

Parameters

  • x − Must be a numeric value.

Return Value

This method returns the angle in degrees.

Example

The following example demonstrates the use of the degrees() method:

from math import degrees, pi

x = 3
val = degrees(x)
print ("x: ",x, "degrees(x): ", val)

x = -3
val = degrees(x)
print ("x: ",x, "degrees(x): ", val)

x = 0
val = degrees(x)
print ("x: ",x, "degrees(x): ", val)

x=pi
val = degrees(x)
print ("x: ",x, "degrees(x): ", val)

x=pi/2
val = degrees(x)
print ("x: ",x, "degrees(x): ", val)

x=pi/4
val = degrees(x)
print ("x: ",x, "degrees(x): ", val)

When you run the above program, the following output is produced.

x: 3 degrees(x): 171.88733853924697
x: -3 degrees(x): -171.88733853924697
x: 0 degrees(x): 0.0
x: 3.141592653589793 degrees(x): 180.0
x: 1.5707963267948966 degrees(x): 90.0
x: 0.7853981633974483 degrees(x): 45.0

Radians() Function

The radians() function converts the angle x from degrees to radians.

Syntax

The syntax of the radians() function is as follows –

radians(x)

Note − This function cannot be accessed directly, so we need to import the math module and then call this function using the math static object.

Parameters

  • x − This must be a number value.

Return Value

This function returns the value of an angle in radians.

Example

The following example shows the use of the radians() method −

from math import radians, pi

x = 30
val = radians(x)
print ("x: ",x, "radians(x): ", val)

x = -30
val = radians(x)
print ("x: ",x, "radians(x): ", val)

x = 0
val = radians(x)
print ("x: ",x, "radians(x): ", val)

x = 90
val = radians(x)
print ("x: ",x, "radians(x): ", val)

x = 180
val = radians(x)
print ("x: ",x, "radians(x): ", val)

x = 45
val = radians(x)
print ("x: ",x, "radians(x): ", val)

When we run the above program, it produces the following output –

x: 30 radians(x): 0.5235987755982988
x: -30 radians(x): -0.5235987755982988
x: 0 radians(x): 0.0
x: 90 radians(x): 1.5707963267948966
x: 180 radians(x): 3.141592653589793
x: 45 radians(x): 0.7853981633974483

Leave a Reply

Your email address will not be published. Required fields are marked *