Skip to content
Related Articles

Related Articles

Improve Article
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






# Python Program to explain math.dist() method
  
# Importing math module
import math
  
# One dimensional Point
  
# Coordinate of Point P
P = 3
  
# Coordinates of point Q
Q = -8
  
# Calculate the Euclidean distance 
# between points P and Q
eDistance = math.dist([P], [Q])
print(eDistance)
Output:
11.0

Code #2:




# Python Program to explain math.dist() method
  
# Importing math module
import math
  
# Two dimensional Point
  
# Coordinates of Point P
Px = 3 
Py = 7
  
# Coordinates of point Q
Qx = -5
Qy = -9
  
# Calculate the Euclidean distance 
# between points P and Q
eDistance = math.dist([Px, Py], [Qx, Qy])
print(eDistance)
  
  
# Three-dimensional point
  
# Coordinates of Point P
P = [3, 6, 9]
  
# Coordinates of point Q
Q = [1, 0, -2
  
# Calculate the Euclidean distance 
# between points P and Q
eDistance = math.dist(P, Q)
print(eDistance)
Output:
17.88854381999832
12.688577540449518

Code #3:




# Python Program to explain math.dist() method
  
# Importing math module
import math
  
# n-dimensional Point
  
# Coordinates of Point P
P = [3, 9, 7, 2, 4, 5
  
# Coordinates of point Q
Q = [-5, -3, -9, 0, 6, 2]
  
# Calculate the Euclidean distance 
# between points P and Q
eDistance = math.dist(P, Q)
print(eDistance)
  
# Dimension of both points 
# should be the same 
Output:
21.93171219946131

Reference: Python math library

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :