What is the math.dist() function in Python?

math.dist() from the math module in Python is used to calculate the Euclidean distance between two points, p and q. To calculate the distance between these points, p and q should have the same dimensions.

sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))

Syntax


math.dist(p,q)

Parameters

Both values should be a single integer or a list of integer values.

  • p: first argument
  • q: second argument

Return value

float-value: This method will return the Euclidean distance between p and q.

Example

This math.dist() method helps to calculate Euclidean distance. Therefore, in line 7 we calculate it using p = [5] and q = [3]. In line 12, however, we use a list of integer values as arguments, i.e., p = [5, 5], q = [3, 6]

# Import math Library
import math
# single integer values
p = [5]
q = [3]
# Calculate Euclidean distance
print (math.dist(p, q))
# -------- p,q as List---------
p = [5, 5]
q = [3, 6]
# Calculate Euclidean distance
print (math.dist(p, q))

Free Resources