Python math.hypot() Method
Example
Find the hypotenuse of a right-angled triangle where perpendicular and base are known:
#Import math Library
import math
#set perpendicular and base
parendicular = 10
base = 5
#print the hypotenuse of a right-angled
triangle
print (math.hypot(parendicular, base))
Try it Yourself »
Definition and Usage
The math.hypot()
method finds the Euclidean norm. The Euclidian norm is the distance from the origin to the coordinates given.
Before Python 3.8 this method could only be used to find the hypotenuse of a right-angled triangle. For two-dimensional cases, where x and y are perpendicular and base, the hypotenuse is calculated by sqrt(x*x + y*y)).
As of Python version 3.8 we can use it to calculate the Euclidean norm as well. For n-dimensional cases, the coordinates passed are assumed to be like (x1, x2, x3, ..., xn). So Euclidean length from the origin is calculated by sqrt(x1*x1 + x2*x2 +x3*x3 .... xn*xn).
For two dimensional points it is still similar to computing hypotenuse of a right-angled triangle.
Syntax
math.hypot(x1, x2, x3, ..., xn)
Parameter Values
Parameter | Description |
---|---|
x1, x2, x3, ..., xn | Required. Two or more numbers representing coordinates |
Technical Details
Return Value: | A float value, representing the Euclidean distance from the origin for n inputs, or hypotenuse of a right-angled triangle for two inputs |
---|---|
Changelog: | As of 3.8, this method compute n-dimensional points. Earlier versions only support 2-dimensional points |
More Examples
Example
Find the Euclidean length of a given coordinates
#Import math Library
import math
#print the Euclidean length
from the origin of given coordinates.
print (math.hypot(10,2,4,13))
print (math.hypot(4,7,8))
print (math.hypot(12,14))