Python math - dist() Function
The Python math.dist() function returns the Euclidean distance between two points p and q, each given as a sequence (or iterable) of coordinates. The two points must have the same dimension. Mathematically, it is equivalent to:
math.dist(p, q) = sqrt(sum((px - qx)**2.0 for px, qx in zip(p, q)))
Syntax
#New in version 3.8 math.dist(p, q)
Parameters
p |
Required. An iterable specifying dimensions of first point. |
q |
Required. An iterable specifying dimensions of second point. |
Return Value
Returns the Euclidean distance between two points p and q.
Example: Distance between 2-D points
In the example below, dist() function returns Euclidean distance between two given points.
import math p = [0, 0] q = [3, 4] print("Distance between p and q: ", math.dist(p, q))
The output of the above code will be:
Distance between p and q: 5.0
Example: Distance between 3-D points
Similarly, the function can be used to find distance between 3-D points.
import math p = [0, 0, 0] q = [3, 4, 5] print("Distance between p and q: ", math.dist(p, q))
The output of the above code will be:
Distance between p and q: 7.0710678118654755
❮ Python Math Module