The Wayback Machine - https://web.archive.org/web/20240712032702/https://www.geeksforgeeks.org/python-math-dist-method/
Open In App

Python – math.dist() method

Last Updated : 23 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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



Similar Reads

fabs() method of Python's math Library
fabs() is a method specified in math library in Python 2 and Python 3. Sometimes while computing the difference between 2 numbers to compute the closeness of a number with the other number, we require to find the magnitude of certain number, fabs() can come handy in the cases where we are dealing with ints and want the result in float number to per
3 min read
Python math library | expm1() method
Python has math library and has many functions regarding to it. One such function is expm1(). This function mathematically computes the value of exp(x) - 1. This method can be used if we need to compute this very value. Syntax : math.expm1() Parameters : x : Number whose exp(x)-1 has to be computed. Returns : Returns the computed value of "exp(x)-1
2 min read
Python math.gamma() Method
Python in its language allows various mathematical operations, which has manifolds application in scientific domain. One such offering of Python is the inbuilt gamma() function, which numerically computes the gamma value of the number that is passed in the function. Syntax : math.gamma(x) Parameters : x : The number whose gamma value needs to be co
2 min read
Python math library | isnan() method
Python has math library and has many functions regarding it. One such function is isnan(). This method is used to check whether a given parameter is a valid number or not. Syntax : math.isnan(x) Parameters : x [Required] : It is any valid python data type or any number. Returns: Return type is boolean. -> Returns False if the given parameter is
1 min read
Python math library | exp() method
Python has math library and has many functions regarding it. One such function is exp(). This method is used to calculate the power of e i.e. e^y or we can say exponential of y. The value of e is approximately equal to 2.71828….. Syntax : math.exp(y) Parameters : y [Required] - It is any valid python number either positive or negative. Note that if
2 min read
Python math library | isfinite() and remainder() method
Python has math library and has many functions regarding to it. math.remainder() method returns an exact (floating) value as a remainder. Syntax: math.remainder(x, y) Time Complexity: O(1) Auxiliary space: O(1) For finite x and finite nonzero y, this is the difference x - n*y, where n is the closest integer to the exact value of the quotient x / y.
1 min read
Python | math.isqrt() method
Math module in Python contains a number of mathematical operations, which can be performed with ease using the module. math.isqrt() method in Python is used to get the integer square root of the given non-negative integer value n. This method returns the floor value of the exact square root of n or equivalently the greatest integer a such that a2
2 min read
Python - math.comb() method
Math module in Python contains a number of mathematical operations, which can be performed with ease using the module. math.comb() method in Python is used to get the number of ways to choose k items from n items without repetition and without order. It basically evaluates to n! / (k! * (n - k)!) when k n. It is also known as binomial coefficient b
2 min read
Python - math.perm() method
Math module in Python contains a number of mathematical operations, which can be performed with ease using the module. math.perm() method in Python is used to get the number of ways to choose k items from n items without repetition and with order. It Evaluates to n! / (n - k)! when k <= n and evaluates to 0 when k > n. This method is new in P
2 min read
Python - math.prod() method
Math module in Python contains a number of mathematical operations, which can be performed with ease using the module. math.prod() method in Python is used to calculate the product of all the elements present in the given iterable. Most of the built-in containers in Python like list, tuple are iterables. The iterable must contain numeric value else
2 min read
three90RightbarBannerImg