AppDividend
Latest Code Tutorials

# Python atan2: How to Find Arctangent in Radians

Python atan2() is an inbuilt method that is defined under the math module, which is used to find the arctangent of y/x in radians. In terms of atan(), we can relate it as atan(y/x) where y and x are numeric values. We can use a math module by importing it. We need to import the math module; after importing, we use to call this function using the static object. It returns the arctangent of the two passed numeric parameters in the form of y/x.

## Python atan2()

Python atan2(y, x) returns value of atan(y/x) in radians. The atan2() method returns a numeric value between – and  representing the angle  of an (x, y) point and the positive x-axis.

### Syntax

math.atan2(y,x)

### Parameters

It takes two parameters out of which both are of numeric datatype; any other data type is not acceptable if any other data type is passed as a parameter; it throws a type error.

### Return Value

It returns the arc tangent value of the number in the float datatype.

X: It can be a number or a valid numerical expression which represent Cartesian X – Coordinate.

Y: It can be a number or a valid numerical expression which represent Cartesian Y – Coordinate.

See the following code example.

import math
x = 0.35
y = 3.25
print(math.atan2(y, x))


#### Output

python3 app.py
1.4635174711464938

### Example programs on atan2() method in Python

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

import math

x1 = 0.32
y1 = 4.33

x2 = 33.52
y2 = 55.34

x3 = 23.22
y3 = 33.11

print("x1= ", x1, "y1= ", y1, "arc= ", math.atan2(y1, x1))
print("x2= ", x2, "y2= ", y2, "arc= ", math.atan2(y2, x2))
print("x3= ", x3, "y3= ", y3, "arc= ", math.atan2(y3, x3))


#### Output

x1= 0.32 y1= 4.33 arc= 1.4970274295011163
x2= 33.52 y2= 55.34 arc= 1.0261887579183147
x3= 23.22 y3= 33.11 arc= 0.9591992824866347


In this example, we have seen that by passing two parameters (x,y) for different examples, get the desired atan2() method solution, which is the arc.

#### Example 2: Write a program to pass a value of any other datatype in the atan2() function and display the output.

import math
x1 = 0.32
y1 = 'a'

print("arc= ", math.atan2(y1, x1))


#### Output

TypeError: must be real number, not str


In this example, we tried passing a parameter, which is not a real number, and because of that, the program throws an error stating that the number must be real and not string.

## Python atan2() with list and tuple

See the following code.

# app.py

import math

Tup = (11, 21, 3, -46, 5)
Lis = [-11, 21, -3.5, -46, 5]

print('Tangent value of Positive Number = %.2f' % math.atan2(2, 4))
print('Tangent value of Negative Number = %.2f' % math.atan2(-1, 6))

print('Tangent value of Tuple Item = %.2f' % math.atan2(Tup[3], Tup[2]))
print('Tangent value of List Item = %.2f' % math.atan2(Lis[2], Lis[4]))

print('Tangent value of Multiple Number = %.2f' % math.atan2(2 + 7 - 4, 9-5))

print('Tangent value of String Number = %.2f', math.atan2('Hello', 'Python'))


#### Output

python app.py
Tangent value of Positive Number = 0.46
Tangent value of Negative Number = -0.17
Tangent value of Tuple Item = -1.51
Tangent value of List Item = -0.61
Tangent value of Multiple Number = 0.90
Traceback (most recent call last):
File "app.py", line 14, in <module>
print('Tangent value of String Number = %.2f', math.atan2('Hello', 'Python'))
TypeError: a float is required

If you pass the string to the atan2() function, then it will give the TypeError, which says: a float is required.