AppDividend
Latest Code Tutorials

Python radians() Function | Math.radians() in Python Example

0

Python radians() is an inbuilt method that is defined under the math module, which is used to convert the angle x( which is the parameter ) from degrees to radians. For example, if x is passed as a parameter in degrees, then the function (radians(x)) returns the radian value of that angle. We can use a math module by importing it. After importing the module, we can call the radians() function using the static object.

Python radians()

Python degrees() and radians() are methods specified in the math module in Python 3 and Python 2. Often one is in need to handle the mathematical computation of conversion of the radians to degrees and vice-versa, especially in the field of geometry.

Syntax

math.radians(var)

Here var is the variable which we have to convert from degrees into radians.

Parameters

It takes one argument var, which takes values of numeric datatype and throws TypeError if the parameter is of any other data type is passed.

Return Value

It returns the radian value of the number in the float datatype.

See the code.

import math

var = 0.36
print(math.radians(var))

Example programs on radians() method in Python

Example 1: Write a program to show the working of the radians() method in Python.

import math

a1 = 0.36
b1 = 1
c1 = -1
d1 = -0.36

print("Value for parameter ", a1, " is ", math.radians(a1))
print("Value for parameter ", b1, " is ", math.radians(b1))
print("Value for parameter ", c1, " is ", math.radians(c1))
print("Value for parameter ", d1, " is ", math.radians(d1))

Output

Value for parameter  0.36  is  0.006283185307179586
Value for parameter  1  is  0.017453292519943295
Value for parameter  -1  is  -0.017453292519943295
Value for parameter  -0.36  is  -0.006283185307179586

In the above example, we have seen that bypassing the valid parameter in the degrees function, we get the value of the angle in radians. Here we have passed different angles and got different values.

Example 2: Write a program to pass a value out of range from the radians() function and display the output.

See the following code.

import math

h = "HellO"
print(math.radians(h))

Output

TypeError: must be real number, not str

In this example, we’ve seen that passing a parameter, which is not a real number, throws a TypeError.

Python radians() with list and tuple

See the following code.

import math

Tup = (45, 20, 90, 40, 30)  # Tuple Declaration
Lis = [90.65, 35.48, 92.65, 30.41, 17.27]  # List Declaration

print('Calculating Radians of Zero in Python = %.2f' % math.radians(0))
print('Calculating Radians of Positive Integer in Python = %.2f' % math.radians(6))
print('Calculating Radians of Negative Integer in Python = %.2f' %
      math.radians(-12))

print('Calculating Radians of Positive Decimal in Python = %.2f' %
      math.radians(4.56))
print('Calculating Radians of Negative Decimal in Python = %.2f' %
      math.radians(-15.78))

print('Calculating Radians of Tuple Item in Python = %.2f' %
      math.radians(Tup[2]))
print('Calculating Radians of List Item in Python = %.2f' %
      math.radians(Lis[2]))

print('Calculating Radians of Multiple Numbers in Python = %.2f' %
      math.radians(45 + 20 - 25))

print('Calculating Radians of PIE in Python = %.4f' % math.radians(math.pi))
print('Calculating Radians of PIE/2 in Python = %.4f' %
      math.radians(math.pi/2))
print('Calculating Radians of PIE/4 in Python = %.4f' %
      math.radians(math.pi/4))
print('Calculating Radians of PIE/6 in Python = %.4f' %
      math.radians(math.pi/6))

print('Calculating Radians of String Value in Python = ', math.radians('3'))

Output

python3 app.py
Calculating Radians of Zero in Python = 0.00
Calculating Radians of Positive Integer in Python = 0.10
Calculating Radians of Negative Integer in Python = -0.21
Calculating Radians of Positive Decimal in Python = 0.08
Calculating Radians of Negative Decimal in Python = -0.28
Calculating Radians of Tuple Item in Python = 1.57
Calculating Radians of List Item in Python = 1.62
Calculating Radians of Multiple Numbers in Python = 0.70
Calculating Radians of PIE in Python = 0.0548
Calculating Radians of PIE/2 in Python = 0.0274
Calculating Radians of PIE/4 in Python = 0.0137
Calculating Radians of PIE/6 in Python = 0.0091
Traceback (most recent call last):
  File "app.py", line 32, in <module>
    print('Calculating Radians of String Value in Python = ', math.radians('3'))
TypeError: must be real number, not str

In the first three statements, we have used the radians function with Zero, Positive integer, and negative integer values as arguments. In the next two statements, we have used the radians function with both the Positive and negative decimal values as parameters.

Next two statements, we used the radians method on Python Tuple and Python List items. If you observe the above output, the radians() function is working correctly on them.

We tried radians() method directly on multiple values. Next, we used the radians() function on Pie values.

In the last code, We used the radians() function on the String value. As we said before, it returns the TypeError as output.

See also

Python tanh()

Python sinh()

Python tan()

Python sin()

Python acosh()

Python asinh()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.