How to calculate distance in python

❮ Math Methods


Example

Find the Euclidean distance between one and two dimensional points:

# Import math Library
import math

p = [3]
q = [1]

# Calculate Euclidean distance
print (math.dist(p, q))

p = [3, 3]
q = [6, 12]

# Calculate Euclidean distance
print (math.dist(p, q))

The result will be:

2.0
9.486832980505138

Run Example »


Definition and Usage

The math.dist() method returns the Euclidean distance between two points (p and q), where p and q are the coordinates of that point.

Note: The two points (p and q) must be of the same dimensions.


Syntax

Parameter Values

ParameterDescription
p Required. Specifies point 1
q Required. Specifies point 2

Technical Details

Return Value:A float value, representing the Euclidean distance between p and q
Python Version:3.8

❮ Math Methods


View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    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


    Last update on August 19 2022 21:50:49 (UTC/GMT +8 hours)

    Python Basic: Exercise-40 with Solution

    Write a Python program to compute the distance between the points (x1, y1) and (x2, y2).

    Pictorial Presentation:

    How to calculate distance in python

    Sample Solution:-

    Python Code:

    import math
    p1 = [4, 0]
    p2 = [6, 6]
    distance = math.sqrt( ((p1[0]-p2[0])**2)+((p1[1]-p2[1])**2) )
    
    print(distance)
    
    

    Sample Output:

    6.324555320336759 
    

    Flowchart:

    How to calculate distance in python

    Visualize Python code execution:

    The following tool visualize what the computer is doing step-by-step as it executes the said program:

    Python Code Editor:

    Have another way to solve this solution? Contribute your code (and comments) through Disqus.

    Previous: Write a Python program to compute the future value of a specified principal amount, rate of interest, and a number of years.
    Next: Write a Python program to check whether a file exists.

    What is the difficulty level of this exercise?

    Test your Programming skills with w3resource's quiz.

    Python: Tips of the Day

    Given each iterable to construct a tuple by adding an index:

    >>> a = ['Hello', 'world', '!']
    >>> list(enumerate(a))
    [(0, 'Hello'), (1, 'world'), (2, '!')]
    


    • Exercises: Weekly Top 16 Most Popular Topics
    • SQL Exercises, Practice, Solution - JOINS
    • SQL Exercises, Practice, Solution - SUBQUERIES
    • JavaScript basic - Exercises, Practice, Solution
    • Java Array: Exercises, Practice, Solution
    • C Programming Exercises, Practice, Solution : Conditional Statement
    • HR Database - SORT FILTER: Exercises, Practice, Solution
    • C Programming Exercises, Practice, Solution : String
    • Python Data Types: Dictionary - Exercises, Practice, Solution
    • Python Programming Puzzles - Exercises, Practice, Solution
    • C++ Array: Exercises, Practice, Solution
    • JavaScript conditional statements and loops - Exercises, Practice, Solution
    • C# Sharp Basic Algorithm: Exercises, Practice, Solution
    • Python Lambda - Exercises, Practice, Solution
    • Python Pandas DataFrame: Exercises, Practice, Solution
    • Conversion Tools
    • JavaScript: HTML Form Validation


    How do I find the distance between 2 points?

    Distance between two points is the length of the line segment that connects the two points in a plane. The formula to find the distance between the two points is usually given by d=√((x2 – x1)² + (y2 – y1)²). This formula is used to find the distance between any two points on a coordinate plane or x-y plane.

    How do you find the distance between two vectors in Python?

    How to Calculate Euclidean Distance in Python? The formula to calculate the distance between two points (x1 1 , y1 1 ) and (x2 2 , y2 2 ) is d = √[(x2 – x1)2 + (y2 – y1)2]. There are 4 different approaches for finding the Euclidean distance in Python using the NumPy and SciPy libraries.