Python – math.dist() method
Math module in Python contains a number of mathematical operations, which can be performed with ease using the module. math.dist() method in Python is used to 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.
This method is new in Python version 3.8.
Syntax: math.dist(p, q)
Parameters:
p: A sequence or iterable of coordinates representing first point
q: A sequence or iterable of coordinates representing second pointReturns: the calculated Euclidean distance between the given points.
Code #1: Use of math.dist() method
# Python Program to explain math.dist() method # Importing math moduleimport math # One dimensional Point # Coordinate of Point PP = 3 # Coordinates of point QQ = -8 # Calculate the Euclidean distance # between points P and QeDistance = math.dist([P], [Q])print(eDistance) |
Output:
11.0
Code #2:
# Python Program to explain math.dist() method # Importing math moduleimport math # Two dimensional Point # Coordinates of Point PPx = 3 Py = 7 # Coordinates of point QQx = -5Qy = -9 # Calculate the Euclidean distance # between points P and QeDistance = math.dist([Px, Py], [Qx, Qy])print(eDistance) # Three-dimensional point # Coordinates of Point PP = [3, 6, 9] # Coordinates of point QQ = [1, 0, -2] # Calculate the Euclidean distance # between points P and QeDistance = math.dist(P, Q)print(eDistance) |
Output:
17.88854381999832 12.688577540449518
Code #3:
# Python Program to explain math.dist() method # Importing math moduleimport math # n-dimensional Point # Coordinates of Point PP = [3, 9, 7, 2, 4, 5] # Coordinates of point QQ = [-5, -3, -9, 0, 6, 2] # Calculate the Euclidean distance # between points P and QeDistance = math.dist(P, Q)print(eDistance) # Dimension of both points # should be the same |
Output:
21.93171219946131
Reference: Python math library






Please Login to comment...