In the previous article, we have discussed Python Program for atan() Function
atan2() Function in Python:
The arc tangent of y/x in radians is returned by the math.atan2() method. In this equation, x and y are the coordinates of a point (x,y).
The value returned is between PI and -PI. (where PI=22/7)
Syntax:
math.atan2(y, x)
Parameters:
y: This is required. It is a number (cartesian y coordinate). Indicates whether the number is positive or negative.
x: This is required. It is a number (cartesian x coordinate). Indicates whether the number is positive or negative.
Return Value:
Returns a float representing the arc tangent of y/x in radians between PI and -PI.
Note: If we pass non-numeric values to the atan2 function, it will return TypeError as output.
Examples:
Example1:
Input:
Given y coordinate = 2 Given x coordinate = 4
Output:
The result after applying atan2() function on above given y and x coordinates { 2 , 4 } = 0.4636476090008061Example2:
Input:
Given y coordinate = 1 Given x coordinate = 3
Output:
The result after applying atan2() function on above given y and x coordinates { 1 , 3 } = 0.3217505543966422Program for atan2() Function in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Import math module using the import keyword.
- Give the list as static input and store it in a variable.
- Give the y-coordinate as static input and store it in another variable.
- Give the x-coordinate as static input and store it in another variable.
- Apply math.atan2() function to the given y-coordinate and the x-coordinate to get the value of arc tangent of given y-coordinate/x-coordinate in radians.
- Store it in another variable.
- Print the above result which is the result after applying math.atan2() function.
- Give the number as static input and store it in another variable.
- Apply math.atan2() function to the given list element and above-given number and print it.
- The Exit of Program.
Below is the implementation:
# Import math module using the import keyword.
import math
# Give the list as static input and store it in a variable.
gvn_lst = [1, -2, 4, 0]
# Give the y-coordinate as static input and store it in another variable.
gvn_ycoordinte = 2
# Give the x-coordinate as static input and store it in another variable.
gvn_xcoordinte = 4
# Apply math.atan2() function to the given y-coordinate and the x-coordinate
# to get the value of arc tangent of given the y-coordinate/x-coordinate in radians.
# Store it in another variable.
fnl_rslt = math.atan2(gvn_ycoordinte, gvn_xcoordinte)
# Print the above result which is the result after applying math.atan2() function.
print("The result after applying atan2() function on above given y and x coordinates {",
gvn_ycoordinte, ",", gvn_xcoordinte, "} = ", fnl_rslt)
# Give the number as static input and store it in another variable.
gvn_numb3 = 2
# Apply math.atan2() function to the given list element and above given number
# and print it.
print("The result after applying atan2() function on the given list element gvn_lst[1] and above given number = ", math.atan2(
gvn_lst[1], gvn_numb3))
Output:
The result after applying atan2() function on above given y and x coordinates { 2 , 4 } = 0.4636476090008061
The result after applying atan2() function on the given list element gvn_lst[1] and above given number = -0.7853981633974483Method #2: Using Built-in Functions (User Input)
Approach:
- Import math module using the import keyword.
- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in a variable.
- Give y-coordinate and x-coordinate respectively as user input using int(), map(), input(), and split() functions.
- Store them in two separate variables.
- Apply math.atan2() function to the given y-coordinate and the x-coordinate to get the value of arc tangent of given y-coordinate/x-coordinate in radians.
- Store it in another variable.
- Print the above result which is the result after applying math.atan2() function.
- Give the number as user input using the int(input()) function and store it in another variable.
- Apply math.atan2() function to the given list element and above-given number and print it.
- The Exit of Program.
Below is the implementation:
# Import math module using the import keyword.
import math
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
'Enter some random List Elements separated by spaces = ').split()))
# Give y-coordinate and x-coordinate respectively as user input using
# int(), map(), input(), and split() functions.
gvn_ycoordinte, gvn_xcoordinte = map(int, input(
"Enter two random numbers separated by spaces = ").split())
# Apply math.atan2() function to the given y-coordinate and the x-coordinate
# to get the value of arc tangent of given the y-coordinate/x-coordinate in radians.
# Store it in another variable.
fnl_rslt = math.atan2(gvn_ycoordinte, gvn_xcoordinte)
# Print the above result which is the result after applying math.atan2() function.
print("The result after applying atan2() function on above given y and x coordinates {",
gvn_ycoordinte, ",", gvn_xcoordinte, "} = ", fnl_rslt)
# Give the number as user input using the int(input()) function and
# store it in another variable.
gvn_numb3 = int(input("Enter some random number = "))
# Apply math.atan2() function to the given list element and above given number
# and print it.
print("The result after applying atan2() function on the given list element gvn_lst[2] and above given number = ", math.atan2(
gvn_lst[2], gvn_numb3))Output:
Enter some random List Elements separated by spaces = 4 5 2 1 0
Enter two random numbers separated by spaces = 1 3
The result after applying atan2() function on above given y and x coordinates { 1 , 3 } = 0.3217505543966422
Enter some random number = 2
The result after applying atan2() function on the given list element gvn_lst[2] and above given number = 0.7853981633974483Know all about Mathematical Functions in Python by going through our Python Mathematical Methods Examples well explained step by step.