Python math.hypot() Method
The math.hypot() method in Python returns the Euclidean norm.
The Euclidean norm is the distance from the origin to the given coordinates.
The Euclidean metric, also known as Euclidean distance, refers to the "ordinary" (i.e., straight-line) distance between two points in Euclidean space.
Before Python 3.8, this method was used to find the hypotenuse of a right-angled triangle: sqrt(x*x + y*y)
.
Starting from Python 3.8, this method is also used to calculate the Euclidean norm. For n-dimensional cases, assuming coordinates are passed like (x1, x2, x3, ..., xn), the Euclidean length from the origin is calculated by sqrt(x1*x1 + x2*x2 + x3*x3 .... xn*xn)
.
Python Version: 3.8
Syntax
The syntax for the math.hypot() method is as follows:
math.hypot(x1, x2, x3, ..., xn)
Parameter Description:
- x1, x2, x3, ..., xn -- Required, representing multiple coordinate points.
Return Value
A floating-point value representing the Euclidean distance from the origin to the n input points, or the hypotenuse of a right-angled triangle with two inputs.
Example
The following example calculates the hypotenuse of a right-angled triangle:
Example
# Import math package
import math
# Set perpendicular and base
perpendicular = 10
base = 5
# Output the hypotenuse of the right-angled triangle
print(math.hypot(perpendicular, base))
Output result:
11.180339887498949
The following example calculates the Euclidean norm for given coordinates:
Example
# Import math package
import math
# Output the Euclidean norm for given coordinates
print(math.hypot(10, 2, 4, 13))
print(math.hypot(4, 7, 8))
print(math.hypot(12, 14))
Output result:
17.0
11.357816691600547
18.439088914585774