Python – math.dist() method
Last Updated :
23 Jan, 2020
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 point
Returns: the calculated Euclidean distance between the given points.
Code #1: Use of math.dist()
method
import math
P = 3
Q = - 8
eDistance = math.dist([P], [Q])
print (eDistance)
|
Code #2:
import math
Px = 3
Py = 7
Qx = - 5
Qy = - 9
eDistance = math.dist([Px, Py], [Qx, Qy])
print (eDistance)
P = [ 3 , 6 , 9 ]
Q = [ 1 , 0 , - 2 ]
eDistance = math.dist(P, Q)
print (eDistance)
|
Output:
17.88854381999832
12.688577540449518
Code #3:
import math
P = [ 3 , 9 , 7 , 2 , 4 , 5 ]
Q = [ - 5 , - 3 , - 9 , 0 , 6 , 2 ]
eDistance = math.dist(P, Q)
print (eDistance)
|
Output:
21.93171219946131
Reference: Python math library
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...