Home » Python

Angular conversion functions in Python

Python Angular conversion functions: Here, we are going to learn about the Angular conversion functions of math module with examples in Python?
Submitted by IncludeHelp, on April 25, 2019

Python Angular conversion functions/methods

In python programming language, there are some of the built-in functions which are defined in math module – they can be used for angular conversion i.e. to convert angle values, there are two angular conversion functions:

  1. math.degrees()
    It is used to convert angle value from radians to degrees.
  2. math.radians()
    It is used to convert angle value from degrees to radians.

Syntax of math.degrees() and math.radians() functions:

    math.degrees(x)
    math.radians(x)

Example:

    Input:
    x = 10.25 # value in radians

    # converting & printing in degrees
    x = math.degrees(x)
    print(x)

    # again, converting & printing in radians
    x = math.radians(x)
    print(x)

    Output:
    587.2817400090938
    10.25

Python code to demonstrate example of angular conversion functions

# Python code to demonstrate example of 
# math.degrees() and math.radians() methods

# importing math module
import math 

# angle x in radians
x = 10.25
print("x in radians: ", x)

# converting to degrees
x = math.degrees(x)
print("x in degrees: ", x)

# now again converting to radians
x = math.radians(x)
print("x in radians: ", x)

Output

x in radians:  10.25
x in degrees:  587.2817400090938
x in radians:  10.25

Note: If we provide anything except a number type value to these functions, they returns a TypeError.

# Python code to demonstrate example of 
# math.degrees() with exception

# importing math module
import math 

# angle x in string (invalid)
x = "10.25"

# converting to degrees
x = math.degrees(x)
print("x in degrees: ", x)

Output

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


Comments and Discussions!

Load comments ↻





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